0

Using Asp.Net MVC 4, I have a requirement to upload a file in two steps:

  1. First page in modal window: pick a file to upload and click submit button. Send the file to the server read first line in the file and return message to client to be displayed on second page.
  2. Second page (in the same modal window): display message returned from first step and if user decides to save file content to server, the file is sent again to server. Read file content and save it to database.

I'm using Ajax to submit and load page1 and page2 partial views and strongly typed viewModel:

public class MyViewModel 
{
     public HttpPostedFileBase MyFile { get; set; }
} 

I have to use JQuery and Ajax as I have to change modal window content with partial views without reloading the hole web page or changing URL.

In order to send the file a second time to server, I need to send the model (that contains the file: HttpPostedFileBase ) back to client which is done via Ajax get action. And again submit the model to server using second Ajax post.

How can I maintain HttpPostedFileBase object content to the second submit?

Mhd
  • 2,778
  • 5
  • 22
  • 59
  • If the `` is on `page1`, and you replacing that with another input on `page2` then you cannot (the value of a file input can only be set by the user selecting a file in the browser). Why do you need to return a 'separate' page (instead of just updating the 1st) –  Dec 01 '17 at 03:00
  • @StephenMuecke After the first submit, some properties of the viewModel are populated from file content. And I have to refresh viewModel and display new populated properties in the second page. That's why I was thinking about using two different partial views. – Mhd Dec 01 '17 at 03:06
  • Yes, but you can just return a `JsonResult` or partial view to update those fields (they might be hidden initially). Alternatively, you could save the file to a temp folder, and when they confirm, move it to the correct folder (you would need to consider what happens if the users abandons it - e.g. you might have a service to remove temp file older than a certain date). You really need to show some of your code to better understand what your doing –  Dec 01 '17 at 03:09
  • Just as a side note, refer [this answer](https://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) for a simpler upload code –  Dec 01 '17 at 04:34

0 Answers0