user1068636 user1068636 - 1 year ago 82
ASP.NET (C#) Question

When uploading a file using <input> element to a C# server side, how can the C# access file attributes like file size or file type?

I have an element file chooser that allows a user to select a file for upload. As soon as the file is selected, it is uploaded to the windows server 2008 and written to disk in a Windows temporary folder. The serverside code is written in C#.

My question: As the file is transfered to the server, is there a way to access attributes of the file being uploaded such as the file size or file type from within C# server code? Would I be able to grab it from an HttpWebRequest or an HttpMultiPartWebRequest or something?

I would appreciate all / any suggestions from the community.

Answer Source

Here's a tutorial on uploading a file in C# using a Web Form:

Obtaining information about the file itself:

Here are some relevant parts from that second linked article:

"You can use the ContentLength property to get the length of the file. You can use the ContentType property to get the MIME content type of the file. In addition, you can use the PostedFile property to access the FileName property, the InputStream property, and the SaveAs method. However, the same functionality is provided by the FileName property, the FileContent property, and the SaveAs method."

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download