如何将整个POST数据转储到ASP.NET中的文件

per*_*ter 6 asp.net postdata request

我目前正在尝试将应用程序从asp.net移植到php,但是我只是碰壁,需要一只手.

我需要将.aspx通过POST收到的所有数据转储到文件中,但我不知道如何做到这一点

有任何想法吗 ?

wom*_*omp 8

您可以使用Request对象的InputStream属性.这将为您提供http请求的原始数据.通常,您可能希望将其作为自定义http处理程序执行此操作,但我相信您可以随时执行此操作.

if (Request.RequestType == "POST")
{
    using (StreamReader reader = new StreamReader(Request.InputStream))
    {
        // read the stream here using reader.ReadLine() and do your stuff.
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 7

如果您只想要POST数据,那么您可以使用Request.Form.ToString()以url编码方式获取所有数据.

if (Request.RequestType == "POST") {
    string myData = Request.Form.ToString();
    writeData(myData); //use the string to dump it into a file,
}
Run Code Online (Sandbox Code Playgroud)


Meh*_*ari 6

您可以使用BinaryRead从请求正文中读取:

Request.BinaryRead
Run Code Online (Sandbox Code Playgroud)

或者您可以使用以下命令获取对输入Stream对象的引用:

Request.InputStream
Run Code Online (Sandbox Code Playgroud)

然后你可以使用CopyStream:

using (FileStream fs = new FileStream(...))
    CopyStream(fs, Request.InputStream);
Run Code Online (Sandbox Code Playgroud)

  • .NET 4.0现在有一个`Stream.CopyTo()`方法. (3认同)