加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在ASP.Net中获取请求内容

发布时间:2020-12-15 21:37:01 所属栏目:百科 来源:网络整理
导读:我有以下代码将文件发布到ASP.net页面: using (var Client = new WebClient()) Client.UploadFile("http://localhost:1625/Upload.aspx",@"TestFile.csv"); 在服务器页面上,我读了请求的内容: var Contents = new byte[Request.InputStream.Length]; Reque
我有以下代码将文件发布到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,(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;测试1 … 5;测试5.
我的问题是如何只获取文件内容而不是标题的整个请求?

解决方法

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

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

   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读