user1068636 user1068636 - 2 months ago 16
ASP.NET (C#) Question

When uploading a file using <input> element to a C# asp.net 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 asp.net community.

Answer

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

https://support.microsoft.com/en-us/kb/323246

Obtaining information about the file itself:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx

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."