本文介绍了如何保存POST请求的内容,在.NET中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端应用程序,使一个 POST 要求 asp.net网址(它是用来uplaod一个文件 .NET )。

I have a client application that makes a POST request to asp.net URL( it is used to uplaod a file to .NET).

如何使asp.net页面接收到这个请求,并将其保存为一个文件?

How to make asp.net page receives this request and save it as a file ?

@达林季米特洛夫我曾尝试你的code,但我得到了 500内部服务器错误如何跟踪它?!

@ Darin DimitrovI have tried your code, but I got 500 internal server error how can I track it ?!

推荐答案

如果客户端使用的multipart / form-data的请求编码(这是标准的文件上传)你可以检索上传的文件从 Request.Files 集合。例如:

If the client uses multipart/form-data request encoding (which is the standard for uploading files) you could retrieve the uploaded file from the Request.Files collection. For example:

protected void Page_Load(object sender, EventArgs e)
{
    foreach (HttpPostedFile file in Request.Files)
    {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }
}

如果您知道客户端使用的名称,你可以直接访问它:

or if you know the name used by the client you could directly access it:

HttpPostedFile file = Request.Files["file"];

如果在另一方面,客户端不使用标准编码的要求,你可能需要从文件中读取的 Request.InputStream

If on the other hand the client doesn't use a standard encoding for the request you might need to read the file from the Request.InputStream.

这篇关于如何保存POST请求的内容,在.NET中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:03
查看更多