Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Matching up multiple uploaded files in ASP.NET

Posted on 2013-12-14
5
Medium Priority
?
235 Views
Last Modified: 2013-12-15
So,
I have a form that has multiple uploads.
(lets say an upload for a cake picture and an upload for the recipe)
eg:
<input type="file" name="cakepic" />
<input type="file" name="recipe" />

On the server, i need to put one file in one directory, and one file in another.
The problem I have is, on the server side, how do I tell the two files apart?
(I can't use the FileName as the user can call the files whatever they wish)

How can I get the "name" attribute from Request.Files?
0
Comment
Question by:DFPITC
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 2000 total points
ID: 39719612
If you are doing something similar to this http://msdn.microsoft.com/en-us/library/system.web.httprequest.files(v=vs.110).aspx

Then the key is the name i.e. "cakepic" or "recipe" - make sense?
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39719638
use the extensions to determine if its a pic,

dont allow image extensions in the recipe upload box and only allow certain image extensions in the image upload box ie jpeg, gif, png

you then know what ones are your images and can save them where you like
0
 

Author Comment

by:DFPITC
ID: 39720508
dale_burrell:
That's what i'm trying to do, however, if I loop over AllKeys, it just returns 0s instead of the name.

It is worth noting that the files upload properly, and I can retrieve the filenames properly, just not the "name" attribute on the input.

I can't tell the files apart using the filename as the "cake" analogy isn't the same and they can be files of any type/extension.

--Client--
(this is done for 3 files, put into a "POST" form with "multipart/form-data")
var tObj = document.createElement("input");
tObj.type = "file";
tObj.name = "file1";
tObj.id = "files_file1";
parentDOM.appendChild(tObj);

Open in new window

--Server--
Dim Files As HttpFileCollection = Request.Files
Dim FKeys() As String = Files.AllKeys
Dim k As Integer
For k = 0 To FKeys.GetUpperBound(0)
Response.Write("Key: " & Server.HtmlEncode(FKeys(k)) & VbCrLf)
Next k

Open in new window


I'm expecting this to return:
file1
file2
file3

However, this returns:
0
0
0
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39720532
OK, some things to try.

First asp.net automatically provides a trace utility, which so long as its turned on in web.config will output very useful trace to yoursite.com/trace.axd (http://msdn.microsoft.com/en-us/library/bb386420.aspx).

Looking at the trace will allow you to see what exactly is being posted back to the server.

I have a sneaking suspicion that dynamically adding the input controls directly to the DOM means that asp.net doesn't recognise them even though they should be correctly posted back.

So you could try a test of putting the file input controls in server side just to see if thats part of the issue.

If you set a breakpoint in your code, remembering it needs to be after the Page_Load event (I think) for everything to be loaded correctly. And then inspect the entire Request object, Request.Form, Request.Files etc.

Thats not an answer, but it should provide the information you need to work out what is happening.
0
 

Author Comment

by:DFPITC
ID: 39720591
Ok, so apparently, my issue was that I was using "file" & id rather than "file" + id.

making it a binary compare, rather than a string append.

Doh.

Awarding points to dale as the question was answered, thanks mate :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question