Solved

Matching up multiple uploaded files in ASP.NET

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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