?
Solved

Matching up multiple uploaded files in ASP.NET

Posted on 2013-12-14
5
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

741 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