我有以下代码将文件发布到 ASP.net 页面:

using (var Client = new WebClient())
    Client.UploadFile("http://localhost:1625/Upload.aspx", @"TestFile.csv");

在服务器页面上,我阅读了请求的内容:
        var Contents = new byte[Request.InputStream.Length];
        Request.InputStream.Read(Contents, 0, (int)Request.InputStream.Length);

这是我在内容中得到的:
-----------------------8cf2bdc76fe2b40
Content-Disposition: form-data; name="file"; filename="TestFile.csv"
Content-Type: application/octet-stream

1;Test 1
2;Test 2
3;Test 3
4;Test 4
5;Test 5
-----------------------8cf2bdc76fe2b40--

实际文件内容只是 1;Test 1 ... 5;Test 5
我的问题是如何只获取文件内容而不是包含标题的整个请求?

最佳答案

试试这个,先获取发布的文件:

 {
    HttpFileCollection files = Request.Files;
    HttpPostedFile file = files[0];
    int filelength = file.ContentLength;
    byte[] input = new byte[filelength ];
    file.InputStream.Read(input, 0, filelength );

   }

关于c# - 如何在 ASP.Net 中获取请求内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11392414/

10-10 23:28
查看更多