将文件上传到网络服务网址

时间:2017-03-08 09:20:54

标签: c# asp.net .net file-upload webforms

我使用以下代码将文件发送到soap webservice方法:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://DomainName.com/WebService.asmx?op=SaveVideoHere");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
  fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
request.ContentLength = fileData.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileData, 0, fileData.Length);

WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);

var result = reader.ReadToEnd();
stream.Dispose();
reader.Dispose();

在Webservice中:

[WebMethod]
public void SaveVideoHere() 
{
    if (HttpContext.Current.Request.Files[0] != null)
    {
        this.Context.Response.ContentType = "application/json; charset=utf-8";
        this.Context.Response.Write(HttpContext.Current.Request.Files[0].FileName);
        return;
    }
}

但是文件没有发送,并且存在此错误

  


Additional information: The remote server returned an error: (500) Internal Server Error.

我必须做什么?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

在我的网络服务中,我使用了以下代码。

if (HttpContext.Current.Request.Files.Count != 0)
{
    if (HttpContext.Current.Request.Files[0].ContentLength > 0)
    {
        string ServerPath = Server.MapPath("/storage/");
        HttpContext.Current.Request.Files[0].SaveAs(ServerPath + HttpContext.Current.Request.Files[0].FileName);
        this.Context.Response.ContentType = "application/json; charset=utf-8";
        this.Context.Response.Write(HttpContext.Current.Request.Files[0].FileName);
        return;
    }
}

我用postman测试了它没关系。我的意思是我的webservice接收文件并将文件保存在路径中。

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebServiceOfServer);
 request.Method = "POST";
 //request.ContentType = "application/x-www-form-urlencoded";
 request.KeepAlive = true;
 request.Credentials = System.Net.CredentialCache.DefaultCredentials;
 var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
 request.ContentType = "multipart/form-data; boundary=" + boundary;

 byte[] fileData = null;
 using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
            {
                fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
            }
            request.ContentLength = fileData.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileData, 0, fileData.Length);

            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);

            var result = reader.ReadToEnd();
            stream.Dispose();
            reader.Dispose();

但是当我使用我的webform项目发送文件时,在我的webservice一侧只存在ContentLength文件,并且无法接收文件。 Request.Files.Count为0。 我的webform项目代码有什么问题?