0

I have a basic form on my Create page in my ASP.NET MVC application. The fields are strongly typed to my model. So, whenever I submit the form, a single record gets inserted into my SQL database. This is fine.

The obstacle I'm encountering is I need to have a file upload control on that same form which will take a file (~50-100mb) and save it to a file share on my company's network. So, the user will fill out the form, choose a file to upload, and when they click "Save", the app will save the form fields to the database, as well as save the uploaded file to a file share.

Should I place all of my logic in the Create action of my controller? Since there will be two separate objects being passed in to my Create action (HttpPostedFileBase file, MyModel model - representing the form fields), how can I accomplish this in the Create action, or do you not recommend that? I honestly have no idea where to even start with this.

QUESTION: How can I do my normal "Create" and save my form fields to the database (I already know how to do this, obviously), in conjunction with saving my uploaded file to a file share somewhere? It'd be easy if the file was saved to the database (not advisable, I know), but it's not.

Mike Marks
  • 10,017
  • 17
  • 69
  • 128
  • 1
    If they are note related, why are they in the same form ? If they are related, why not treat all in same Post Action ? – Raphaël Althaus Sep 05 '13 at 16:41
  • @RaphaëlAlthaus So you're suggesting making the file a part of my model and when the HttpPost request comes through, handle it then, if they're related to each other? – Mike Marks Sep 05 '13 at 16:44
  • Well, yes, it seems rather logical, no ? – Raphaël Althaus Sep 05 '13 at 17:10
  • I hardly understand this question. Seems that you both understand how to define your controller's action signature and what you should do in the action. What exactly is the problem you're having? – Admir Tuzović Sep 05 '13 at 17:28
  • @BarisaPuter Sorry for the confusion - I just need to know what the best practice for this is when I'm dealing with an entity separate from my data model but exists within the same form. – Mike Marks Sep 05 '13 at 17:51
  • 1
    I still don't understand the question. If the performance is the issue, try using wherever you can async methods and have your action wait for all. Currently EF does not have async methods, but those should arrive with EF 6 (I just assumed you're using EF since you're using MVC 4). With that you might improve performance because things will be done in parallel. If you're worried about scenario where file is saved to your shared folder but database saving has failed - read about transaction scope and how to set that either everything is saved, or nothing (declare rollback). – Admir Tuzović Sep 06 '13 at 04:29

0 Answers0