在httpwebrequest文件上载中启用POST

在httpwebrequest文件上载中启用POST,post,file-upload,iis-7,httpwebrequest,Post,File Upload,Iis 7,Httpwebrequest,我需要在服务器上上传文件(具有不同的扩展名,例如.txt、.jpg、.pdf…)。 我创建了一个站点,它接受http请求,并将虚拟目录映射到物理目录。 所有这些在dowload中都很好,现在我必须实现上传 这是我的密码 private void UploadFile(string uploadFileName, string localFileName) { //long length = 0; string boundary = "-----------

我需要在服务器上上传文件(具有不同的扩展名,例如.txt、.jpg、.pdf…)。 我创建了一个站点,它接受http请求,并将虚拟目录映射到物理目录。 所有这些在dowload中都很好,现在我必须实现上传

这是我的密码

private void UploadFile(string uploadFileName, string localFileName)
    {
        //long length = 0;
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
        HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uploadFileName);
        httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
        httpWebRequest2.Method = "POST";
        httpWebRequest2.KeepAlive = true;
        httpWebRequest2.Credentials = new NetworkCredential("USER", "PASSWORD", "DOMAIN"); 

        Stream memStream = new System.IO.MemoryStream();

        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +boundary + "\r\n");

        string formdataTemplate = "\r\n--" + boundary +"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

        memStream.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

        string header = string.Format(headerTemplate, "uplTheFile", localFileName);

        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

        memStream.Write(headerbytes, 0, headerbytes.Length);

        FileStream fileStream = new FileStream(localFileName, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[1024];

        int bytesRead = 0;

        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            memStream.Write(buffer, 0, bytesRead);
        }

        memStream.Write(boundarybytes, 0, boundarybytes.Length);

        fileStream.Close();

        httpWebRequest2.ContentLength = memStream.Length;

        Stream requestStream = httpWebRequest2.GetRequestStream();
        //error returned in lenght field: "This stream does not support seek operations."

        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer, 0, tempBuffer.Length);
        requestStream.Close();

        WebResponse webResponse2 = httpWebRequest2.GetResponse();
        //error returned from getResponse: "The remote server returned an error: (405) Method Not Allowed."
        //I guess cause my folder is in read only

        Stream stream2 = webResponse2.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);

        MessageBox.Show(reader2.ReadToEnd());

        webResponse2.Close();
        httpWebRequest2 = null;
        webResponse2 = null; 

    }
首先,我得到了这个错误:远程服务器返回了一个错误:(405)方法不允许。 因此,我试图通过在网站上添加映射来启用POST

现在在te服务器上的文件夹中,我有一个web.config文件:

    <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
    <system.webServer>
    <directoryBrowse enabled="true" />
    <handlers>
        <add name="File TXT" path="*.*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\System32\inetsrv\asp.dll" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
    </handlers>
</system.webServer>
</configuration>

这样我不会得到任何错误,但应用程序不会上载任何文件。
如何解决此问题?

一个更简单的解决方案是使用System.Net.Webclient类上载文件

System.Net.WebClient client = new System.Net.WebClient();

client.UploadFile("www.myfileuploadendpoint.com",@"c:\myfiletoupload.txt");
客户端还有一个OnUploadFileCompleted事件,该事件在完成时将调用处理程序

client.OnUploadFileCompleted  += UploadCompleted(sender,args) => { //do whatever you want}

这将为您节省代码和bug。祝你好运!:)

上述两个样本均不起作用

  • “远程服务器返回错误:(405)不允许使用方法。”尝试上述两个代码时是否捕获异常
  • 请期待有人分享他们对这些例外情况的看法

    rgds,
    thiru

    你不能保存文件吗?您正在使用Asp.NET文件上载控件吗?您需要将文件发送到另一个端点吗?您所说的“只保存文件”是什么意思?我需要把它从客户端上传到服务器文件夹。不,我没有使用Asp.NET,只是应用程序池中的一个网站被httpwebrequest从客户端调用(不确定我是否完全回答了您的问题)得到了完全相同的行为。1) 不允许发布,并且在添加web之后。确认没有错误,但没有上传文件。尽管这可能与“GET”相关,但它表示这是一个wron动词,并且实际上是非常值得期待的。我想我一般需要上传文件或数据