【问题标题】:enabling POST in httpwebrequest file upload在 httpwebrequest 文件上传中启用 POST
【发布时间】:2012-10-24 09:46:39
【问题描述】:

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

这是我的代码

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>

以这种方式我没有收到任何错误,但应用程序没有上传任何文件。 我该如何解决这个问题?

【问题讨论】:

  • 不能只保存文件吗?你在使用 Asp.NET 文件上传控件吗?您需要将文件发送到另一个端点吗?
  • “只保存文件”是什么意思?我需要将它从客户端上传到服务器文件夹。不,我没有使用 Asp.NET,只是应用程序池中的一个网站,由 httpwebrequest 从客户端调用(不确定我是否完全回答了您的问题)

标签: post file-upload iis-7 httpwebrequest


【解决方案1】:

一个更简单的解决方案是使用 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}

这将节省您的代码和错误。祝你好运! :)

【讨论】:

  • 得到了完全相同的行为。 1) POST 不允许,添加 web.confing 后没有错误但没有上传文件
  • 认为这可能是相关的stackoverflow.com/questions/2436182/…
  • "GET" 说这是一个错误的动词,实际上是意料之中的。我想我一般需要 POST 来上传文件或数据
【解决方案2】:

上述两个示例都不起作用,

  1. “远程服务器返回错误:(405) 方法不允许。”是在尝试上述两种代码时发现的异常

希望有人分享他们对这些例外的看法,拜托。

rgds, 直通

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 2012-03-02
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多