使用多部分实体将大型文件从android上传到c#asmx web服务
我通过多部分实体请求从android获取请求数据。如何接受该请求并在服务器端保存文件。请检查已尝试的代码。来自android的文件是视频文件使用多部分实体将大型文件从android上传到c#asmx web服务,c#,asp.net,web-services,asmx,multipartentity,C#,Asp.net,Web Services,Asmx,Multipartentity,我通过多部分实体请求从android获取请求数据。如何接受该请求并在服务器端保存文件。请检查已尝试的代码。来自android的文件是视频文件 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public UploadFileResponse FileUpload(FileStream stream) { JavaScriptSerializer js = ne
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public UploadFileResponse FileUpload(FileStream stream)
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
UploadFileResponse _response = null;
bool IsFileUploaded = false;
if (_response != null)
{
return _response;
}
else
{
_response = new UploadFileResponse();
}
try
{
MultipartParser parser = new MultipartParser(stream);
if (parser.Success)
{
string fileName = parser.Filename;
string contentType = parser.ContentType;
byte[] fileContent = parser.FileContents;
FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
fileToupload.Write(fileContent, 0, fileContent.Length);
fileToupload.Close();
fileToupload.Dispose();
_response.Result = true;
_response.Message = "Success";
stream.Close();
}
else
{
_response.Result = false;
_response.Message = "Oops, something went wrong, please try again.";
}
}
catch (Exception ex)
{
_response.Result = false;
_response.Error = ex.Message;
_response.Message = "Oops, something went wrong, please try again.";
}
finally
{
}
return _response;
}
如果您成功地将多部分数据发送到web服务,则应该能够使用HttpContext.Current.Request捕获传入文件 下面的代码将文件保存到web服务所在的当前目录
[WebMethod]
public void AttachFiles()
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
using (var fileStream = new System.IO.FileStream(AppDomain.CurrentDomain.BaseDirectory+file.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
file.InputStream.CopyTo(fileStream);
}
}
你好。。。我在这一行得到以下异常:HttpPostedFile file=HttpContext.Current.Request.Files[0];当我打发球时。”索引超出范围。必须为非负数且小于集合的大小。参数名称:索引'