I'm working on a MVC3 project; and need to allow user to upload multiple files in 1 form (from the view), then save them to respective folders, based on whether this file uploaded is an image (gif, jpeg, png); or if it is a video file (.swf, .wmv, .mp4,
etc.).
I searched around, and found Phil Hacck's post here: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
and many other people's posts with a similar solution to Phil's code.
But this does not solve my issue - I need to know:
out of 4 files uploaded (2 image + 2 video), which file goes with which <input> field; so I need to be able to differentiate the file based on the "Name" attribute from the <input> field; then decide which folder I should save this specific file at.
In my controller, I need to detect which file goes with which field (e.g. "ImageUpload1"), then save them under totally seperate folder; so I can not just use a foreach loop and save all files under the same folder, as in Phil's example.
I'm sure that there is a way to associate the file with the "name" attribute - can anyone provide some insight?
I tried, but hit this error message when I run the app (F5):
System.NullReferenceException: Object reference not set to an instance of an object.
The reason could be: the 4 upload files that I'm expecting, any of them could be NULL; none of these 4 uploads is required.
My understanding is: when you pass "ImageUpload1" and others to the Action in this fashion, it is expected (required),
correct?
What I want to achieve is: out the 4 files that could be uploaded, my controller need to handle if none is uploaded,
or all are uploaded, and any possible combination in between.
This happens on the first line of writing the 1st input field (from FormCollection) to the db, in the beginning of the action code.
public ActionResult Confirm(HttpPostedFileBase ImageUpload1, HttpPostedFileBase VideoUpload1,
HttpPostedFileBase ImageUpload2, HttpPostedFileBase VideoUpload2, FormCollection ce)
{
// find the right product row in the Product table
Product p = db.Products.Find(Session["PID"]);
// write info into this row, for all non-upload form inputs p.Message1 = ce["Message1"];
.....
Note: the code looks like this: I write all the inputs from the formcollection to the proper fields first; then deal with the 4 upload 1-by-1.
To me it looks like either your Product - p is null or the Key from FormCollection doesn't have the value. I would suggest to do away with FormCollection and create model for that.
Well, I did create a new ViewModel for it, and using the model now; however, I get the same error message for the same line of code.
I put a breakpoint on the line that find the Product row in the table, wanting to see if p is null or not; somehow when I use debug (F5), the app did not stop at that line, but went straight to the line below it and gave the error message....not sure why?
I'm trying to figure out, but here is the stack trace, with more info - hope this can help. Thanks!
This looks like a generic error and doesn't provide much info. I would suggest once you enter into the action method then open the quick watch in visual studio debugger and see what is the value your Session["PI"] has and what is the value p has?
claudia888
Member
180 Points
429 Posts
How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 04:30 PM|LINK
HI,
I'm working on a MVC3 project; and need to allow user to upload multiple files in 1 form (from the view), then save them to respective folders, based on whether this file uploaded is an image (gif, jpeg, png); or if it is a video file (.swf, .wmv, .mp4, etc.).
I searched around, and found Phil Hacck's post here: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
and many other people's posts with a similar solution to Phil's code.
But this does not solve my issue - I need to know: out of 4 files uploaded (2 image + 2 video), which file goes with which <input> field; so I need to be able to differentiate the file based on the "Name" attribute from the <input> field; then decide which folder I should save this specific file at.
In my view, I have:
<div class="editor-field"> <input type="file" name="ImageUpload1" style="width: 520px;" /></div>
...
<div class="editor-field"> <input type="file" name="VideoUpload1" style="width: 520px;" /> </div>
In my controller, I need to detect which file goes with which field (e.g. "ImageUpload1"), then save them under totally seperate folder; so I can not just use a foreach loop and save all files under the same folder, as in Phil's example.
I'm sure that there is a way to associate the file with the "name" attribute - can anyone provide some insight?
Thanks,
Claudia
CPrakash82
All-Star
18154 Points
2831 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 06:00 PM|LINK
Create an action method with matching names argument like this.
public ActionResult Upload(HttpPostedFileBase ImageUpload1, HttpPostedFileBase VideoUpload1) { }
claudia888
Member
180 Points
429 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 06:04 PM|LINK
Well, I have other form inputs that I need to consider for the same form as well...
Is it correct to use this?
public ActionResult Upload(HttpPostedFileBase ImageUpload1, HttpPostedFileBase VideoUpload1, HttpPostedFileBase ImageUpload2, HttpPostedFileBase VideoUpload2, FormCollection form)
{ }
Thanks,
Claudia
CPrakash82
All-Star
18154 Points
2831 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 06:14 PM|LINK
It should be fine.
claudia888
Member
180 Points
429 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 08:38 PM|LINK
Hi,
I tried, but hit this error message when I run the app (F5):
System.NullReferenceException: Object reference not set to an instance of an object.
The reason could be: the 4 upload files that I'm expecting, any of them could be NULL; none of these 4 uploads is required.
My understanding is: when you pass "ImageUpload1" and others to the Action in this fashion, it is expected (required), correct?
What I want to achieve is: out the 4 files that could be uploaded, my controller need to handle if none is uploaded, or all are uploaded, and any possible combination in between.
Any input?
Thanks,
Claudia
CPrakash82
All-Star
18154 Points
2831 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 09:08 PM|LINK
Can provide some code and the location where it is coming?
No it is not, it matches by the name of the input tags .
claudia888
Member
180 Points
429 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 09:37 PM|LINK
This happens on the first line of writing the 1st input field (from FormCollection) to the db, in the beginning of the action code.
public ActionResult Confirm(HttpPostedFileBase ImageUpload1, HttpPostedFileBase VideoUpload1,
HttpPostedFileBase ImageUpload2, HttpPostedFileBase VideoUpload2, FormCollection ce)
{
// find the right product row in the Product table
Product p = db.Products.Find(Session["PID"]);
// write info into this row, for all non-upload form inputs
p.Message1 = ce["Message1"];
.....
Note: the code looks like this: I write all the inputs from the formcollection to the proper fields first; then deal with the 4 upload 1-by-1.
Please let me know.
Thanks,
Claudia
CPrakash82
All-Star
18154 Points
2831 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 09:46 PM|LINK
To me it looks like either your Product - p is null or the Key from FormCollection doesn't have the value. I would suggest to do away with FormCollection and create model for that.
claudia888
Member
180 Points
429 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 03, 2012 10:26 PM|LINK
Well, I did create a new ViewModel for it, and using the model now; however, I get the same error message for the same line of code.
I put a breakpoint on the line that find the Product row in the table, wanting to see if p is null or not; somehow when I use debug (F5), the app did not stop at that line, but went straight to the line below it and gave the error message....not sure why?
I'm trying to figure out, but here is the stack trace, with more info - hope this can help. Thanks!
CPrakash82
All-Star
18154 Points
2831 Posts
Re: How to upload multiple files in 1 form, with different Names for input, in MVC3?
Nov 04, 2012 12:01 AM|LINK
This looks like a generic error and doesn't provide much info. I would suggest once you enter into the action method then open the quick watch in visual studio debugger and see what is the value your Session["PI"] has and what is the value p has?