我正在使用C#创建一个文件上传服务。我创建了三个方法:
upload_start(string filename)upload_continue(string filename, byte[] buffer)upload_end(string filename)
它可以工作,但我不想处理客户端程序中的3个函数。如何从客户端程序打开FileStream,让服务器端完成文件上传?
发布于 2011-05-21 18:46:22
最简单的方法可能是使用wcf或使用asp.net创建REST服务,但是您只需执行POST操作,服务器就可以完成这项工作。
以下是一种方法:
http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/
这将为您提供一个更简单的界面。
发布于 2011-05-21 03:49:57
我不认为你可以,因为FileStream是不可序列化的。为什么不将文件名传递给服务(就像您已经知道的那样),让服务打开文件并处理它。
发布于 2014-10-18 08:06:25
要在WebServices(不是)中上传一个大文件,请遵循以下方法:
在服务器端文件Web.Config中,添加此xml并根据上传的最大大小更改maxRequestLength的值,在本例中,maxRequestLength为4MB。
<httpRuntime
executionTimeout="600"
maxRequestLength="8192"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
enableVersionHeader="true"
/>在服务器端添加另一个公共函数,此函数接收到的byte[]包含文件、文件名和路径,您不会将文件保存在服务器中。
public String UploadFile(byte[] fileByte, String fileName, String savePath)
{
string newPath = savePath;
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
newPath = newPath + fileName;
System.IO.FileStream fs1 = null;
byte[] b1 = null;
b1 = fileByte;
fs1 = new FileStream(newPath, FileMode.Create);
fs1.Write(b1, 0, b1.Length);
fs1.Flush();
fs1.Close();
fs1 = null;
return newPath;
}在客户端,添加以下代码以发送文件:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Select a File";
dlg.DefaultExt = ".xls";
dlg.Filter = "Excel documents (.xls)|*.xls";
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
string filename = dlg.FileName;
string file = Path.GetFileName(filename);
System.IO.FileStream fs1 = null;
fs1 = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read);
byte[] b1 = new byte[fs1.Length];
fs1.Read(b1, 0, (int)fs1.Length);
fs1.Close();
String savePath = @"C:\DOC\IMPORT\";
String newPath = WEB_SERVICES.UploadFile(b1, file, savePath);
MessageBox.Show(String.Format("The file is uploaded in {0}", newPath));
}
else
{
MessageBox.Show("Select a file, please");
}https://stackoverflow.com/questions/6079445
复制相似问题