0

I have N files being uploaded to this application. the names of each are unique, but i dont know how to go about getting the byte array based on a certain name.

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

and on the server would have something like

byte[] data = Request.Files["file1"]; //doesnt work, as i believe it wont take a string, but only an int index 

When i was looking up these solutions, the FileName doesnt really care that much, so Files[0].FileName will not help, nor will indexing. I was hoping to be able to reference the contents based on the name like above.

I wasnt sure where i could find this out. Was looking around MSDN and on here.

I have a lot of files, each with a unique name.... then based on the unique name, which actually contains some additional information, will tell me what to do with it.

It doesnt seem as easy as it is in PHP.

4
  • Please edit your sample so it actually compiles - Item returns HttpPostedFile, not byte[] - HttpFileCollection.Item Commented Sep 6, 2012 at 20:10
  • Indexing by name should work... Are you sure your files are being posted to the server? If you debug and do Request.Files.Count what is returned? Commented Sep 6, 2012 at 20:12
  • let me check and confirm Commented Sep 6, 2012 at 20:14
  • my regex is wrong, so it is taking me a bit to debug and fix. Trying to get a regex that will return on the numbers inside a bracket, but there is a chance that there is also letters in the brackets. some[123] and some[some1234] are both things that need to be read and properly extract the number from. Commented Sep 6, 2012 at 20:34

1 Answer 1

1

Something like this:

 byte[] buffer = new byte[Request.Files["file1"].ContentLength]; Request.Files["file1"].InputStream.Read(buffer,0,buffer.Length); 

And to iterate through all of them:

foreach (var item in Request.Files.AllKeys) { byte[] buffer = new byte[Request.Files[item].ContentLength]; Request.Files[item].InputStream.Read(buffer, 0, buffer.Length); //do something with the byte[] - buffer } 

Update: Interesting... I just ran a test and it only sees the Request.Files collection populated if I set the runat="server" to the file elements: <input type="file" runat="server" name="file1" />

Update 2: Make sure you set enctype="multipart/form-data" on the form if you want to be able to go through the Request.Files collection using regular html input:file elements.

Sign up to request clarification or add additional context in comments.

7 Comments

That is an odd update. That sort of doesnt make sense, as it could be posting to any sort of page, PHP extension among them.
@Fallenreaper Odd as hell! :/ I am doing more testing to see if I can figure out why...
Adding runat="server" changes the form to enctype="multipart/form-data". Without this, only the file name is posted.
@user1429080 you are right on the money. I didn't realize that I didn't have enctype="multipart/form-data until I looked at the rendered source. The problem was that I had been testing on an aspx that ONLY had regular html elements.
@Fallenreaper be warned: image data types will be removed from future versions of SQL Server. As far as storing the bytes. you should be able to store the byte[] in an image column. An image column simply stores bytes. You don't have to convert it to an Image object to be able to store anything in there.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.