kala-tamin Multiple file upload association in MVC
صفحه اصلي

Multiple file upload association in MVC

I have implemented the File Upload (upon reading Scott Hanselman's excellent post)

I have multiple files associated with various questions on the form though, and would like to associate each saved file with an internal ID.

How can I do this? For example, if question # 3 has a file uploaded abc.pdf, how can I associated that file with ID #3?

Any good ideas, or has someone done this before?

Is it possible to map complex types with SolrNet


Resolve Array with Ninject
I would have an array or vector in one of files with a receive ter and setter. How to use AJAX Post to invoke ASP.NET MVC Controller action and return new View?This way when question #3 has file abc.pdf uploaded you must send the information you want to save to the setter and save it at index 3. using different site.master files for themesWhen you want to access it use the receive ter for index 3.. How to display Images using HtmlHelper Class- ASP.NET MVC Depending what you want to save you create an array this holds what you want. asp.net mvc 2 custom service accountI haven't used Asp.net although this site tells you how to sort an array, which i don't want, although it also shows how to make an array of structures. How can I Model Bind to a property that exists in my ViewModel?So if you want to save the name of the file only then you only need a string array. MVC2 validates model also there are no validation attributes to propertiesBut if you need to save the name and any thing else then create the array of structures. .
Private  Structure FileInfo     Public Name As String     Public OtherInfo As String End Structure 
Then create the array with : .
Dim FileInfoArray(NumOfPotentialUploadedFiles - 1) As FileInfo 
Since it sounds like each of your input fields upload one file each you would just need to remember the id number of the fields and then you would easily "know which IDs the uploaded files were associated with" as if field 1 has an uploaded file then it would be in the array at the same position. You could create a boolean within the structure this is set to false when you first create the array. Then when you upload a file of index 1 you change the boolean to true. This way you easily know which files you have when you go through the array b/c only the positions with a true value have a file. .


Ok, figured out an easy solution. I was struggling since the Request.Files[x] object did not have any reference to the fields, although the Request.Files (HttpFileCollectionWrapper) has an AllKeys property this holds the array of fields. My code now is:.
   for (int fileIndex = 0; fileIndex < Request.Files.Count; fileIndex++)    {           string fieldName = Request.Files.AllKeys[fileIndex];   <---  Here is where you must gleam an key to persist to the database, I have an ID in the fieldName           string savedFileName = Path.GetFileName(Request.Files[fileIndex].FileName);           var path = Path.Combine(<your server save path>, savedFileName);           Request.Files[fileIndex].SaveAs(path);     } 
Easy enough!.

77 out of 100 based on 42 user ratings 437 reviews