借助网络服务将文件上传到网络服务器很容易。这是我通常这样做的方式。这是我的示例代码。
[WebMethod]
public bool UploadFile(string FileName, byte[] buffer, long Offset)
{
bool retVal = false;
try
{
// setting the file location to be saved in the server.
// reading from the web.config file
string FilePath =
Path.Combine(ConfigurationManager.AppSettings["upload_path"], FileName);
if (Offset == 0) // new file, create an empty file
File.Create(FilePath).Close();
// open a file stream and write the buffer.
// Don't open with FileMode.Append because the transfer may wish to
// start a different point
using (FileStream fs = new FileStream(FilePath, FileMode.Open,
FileAccess.ReadWrite, FileShare.Read))
{
fs.Seek(Offset, SeekOrigin.Begin);
fs.Write(buffer, 0, buffer.Length);
}
retVal = true;
}
catch (Exception ex)
{
//sending error to an email id
common.SendError(ex);
}
return retVal;
}
但我想开发 Web 服务,它会给我以百分比上传文件的状态,当文件上传完成时,无论文件是否完全上传,都会在客户端触发一个带有状态消息的事件。我还需要编写可以同时处理多个请求的例程,并且例程必须是线程安全的。所以请指导我如何设计足以满足我所有要求的例程。谢谢