Solved

Matching up multiple uploaded files in ASP.NET

Posted on 2013-12-14
5
201 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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now