Html 在asp.net中访问服务器端的输入类型文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:44:54  来源:igfitidea点击:

Accessing input type file at server side in asp.net

asp.nethtmlfile-upload

提问by Ajay

I am using the <input type="file" />tag to upload a file to the server. How do I access the file at the server side and store it at the server? (The file is an image file)

我正在使用<input type="file" />标签将文件上传到服务器。如何在服务器端访问文件并将其存储在服务器上?(该文件是一个图像文件)

The client side code is :

客户端代码是:

<form id="form1" action="PhotoStore.aspx" enctype="multipart/form-data">
    <div>
    <input type="file" id="file" onchange="preview(this)" />
    <input type="submit" />
    </div>
</form>

Photostore.aspx.cs has

Photostore.aspx.cs 有

protected void Page_Load(object sender, EventArgs e)
        {
            int index = 1;
            foreach (HttpPostedFile postedFile in Request.Files)
            {
                int contentLength = postedFile.ContentLength;
                string contentType = postedFile.ContentType;
                string fileName = postedFile.FileName;

                postedFile.SaveAs(@"c:\test\file" + index + ".tmp");

                index++;
            } 

        }

I tried uploading a jpg file. Not able to see a saved file. What is going wrong?

我尝试上传一个 jpg 文件。无法查看已保存的文件。出了什么问题?

回答by LukeH

You'll need to add idand runat="server"attributes like this:

你需要添加idrunat="server"属性,就像这样:

<input type="file" id="MyFileUpload" runat="server" />

Then, on the server-side you'll have access to the control's PostedFileproperty, which will give you ContentLength, ContentType, FileName, InputStreamproperties and a SaveAsmethod etc:

然后,在服务器端,您将可以访问控件的PostedFile属性,该属性将为您提供ContentLengthContentTypeFileNameInputStream属性和SaveAs方法等:

int contentLength = MyFileUpload.PostedFile.ContentLength;
string contentType = MyFileUpload.PostedFile.ContentType;
string fileName = MyFileUpload.PostedFile.FileName;

MyFileUpload.PostedFile.Save(@"c:\test.tmp");

Alternatively, you could use Request.Fileswhich gives you a collection of all uploaded files:

或者,您可以使用Request.Files它为您提供所有上传文件的集合:

int index = 1;
foreach (HttpPostedFile postedFile in Request.Files)
{
    int contentLength = postedFile.ContentLength;
    string contentType = postedFile.ContentType;
    string fileName = postedFile.FileName;

    postedFile.Save(@"c:\test" + index + ".tmp");

    index++;
}

回答by zod

I think the name tag is required on the file input:

我认为文件输入需要名称标签:

<input type="file" name="file" />

Without this, I don't get anything back.

没有这个,我什么也得不到。



Further problems that I had which might be specific to my machine:

我遇到的其他问题可能特定于我的机器:

I get a

我得到一个

Unable to cast object of type 'System.String' to type 'System.Web.HttpPostedFile'.

error at the line

线路错误

foreach (HttpPostedFile postedFile in Request.Files)

so my final code looks like this:

所以我的最终代码如下所示:

for (var i = 0; i < Request.Files.Count; i++)
{
    var postedFile = Request.Files[i]; 

    // do something with file here
}

回答by Dimi Takis

Look into the asp:FileUpload control provided by ASP.NET.

查看 ASP.NET 提供的 asp:FileUpload 控件。

回答by Mark Redman

If you dont want to use the FileUpload control in the toolbox, Give your input an ID then use form[id] to access the input field and cast it to HtmlInputFile.

如果您不想使用工具箱中的 FileUpload 控件,请为您的输入指定一个 ID,然后使用 form[id] 访问输入字段并将其转换为 HtmlInputFile。

example here: http://www.codeproject.com/KB/aspnet/fileupload.aspx

这里的例子:http: //www.codeproject.com/KB/aspnet/fileupload.aspx

回答by Rune Grimstad

If you give the input-tag an id and add the runat="server" attribute then you can access it easily.
First change your input tag: <input type="file" id="FileUpload" runat="server" />
Then add the following to your Page_Load method:

如果你给 input-tag 一个 id 并添加 runat="server" 属性,那么你可以很容易地访问它。
首先更改您的输入标签:<input type="file" id="FileUpload" runat="server" />
然后将以下内容添加到您的 Page_Load 方法中:

if (FileUpload.PostedFile != null) 
{
  FileUpload.PostedFile.SaveAs(@"some path here");
}

This will write your file to a folder of your choice. You can access the PostedFile object if you need to determine the file type or original file name.

这会将您的文件写入您选择的文件夹。如果需要确定文件类型或原始文件名,可以访问 PostedFile 对象。