0

I'm looking to do something like this:-

  • View1 contains a form with a file upload control, for the user to upload an image. This gets POSTed to the controller, where the image is resized and converted to greyscale (the new image is unlikely to be more than 5kb in size).

  • I then want to redirect to View2 where I display this new image, along with a "save" button. Clicking this will result in the image being saved to a DB.

What I'm not sure about is how best to pass the image around. I wondered about doing something like this:-

  • After the image has been manipulated in the View1 POST method, the image data is stored in a model and passed to View2 using RedirectToAction()
  • The View2 GET method simply passes this model to the view, so it can be displayed. The image data is also stored in a hidden field in the view.
  • When the user clicks "save" this is POSTed back, where the controller saves it to the DB.

I'm new to MVC, so am wondering if this an acceptable solution? It doesn't feel quite right "round-tripping" the image data in this way, and wondered if there is a more suitable approach or mechanism (e.g. TempData or session state)?

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • Are you trying to store the image in a database or just in a folder on the server? – BillRuhl Jul 30 '15 at 21:52
  • 1
    Save the image in a temp folder, and load it again in the Action that returns View2, or use ajax, so you don't have to use two different views. – ataravati Jul 30 '15 at 22:02
  • @BillRuhl it will eventually be to a database. – Andrew Stephens Jul 31 '15 at 08:47
  • @ataravati that was my other thought, although it's a little more complex, e.g. needing some kind of housekeeping process that periodically deletes the temp images. I suppose I was trying to keep things simple (read: lazy) by moving the image around as "transient" data, and only persist it as a result of that final button click. – Andrew Stephens Jul 31 '15 at 08:50
  • @AndrewStephens, I wouldn't add an image to the session. Deleting the temp data shouldn't be a problem. You can delete it right after saving the image in the database. – ataravati Jul 31 '15 at 15:07
  • @ataravati it was more of an issue deleting temp images left lying around where the user never clicks the "save" button. It would have to be some process that happens periodically, but it's not the end of the world to implement. – Andrew Stephens Jul 31 '15 at 16:12

1 Answers1

0

You can probably use ViewData. Your code would be something like below.

In your first Action: ViewData["ImageURL"] = YourImageResourceURL;

after redirect, in your view page, you have <img src="@ViewData["ImageURL"]" />

Shawn Song
  • 301
  • 1
  • 9
  • I thought ViewData could only be passed between controller and current view? From what I've read it looks like TempData is better suited for redirects (I found this article: http://stackoverflow.com/questions/1500402/when-to-use-tempdata-vs-session-in-asp-net-mvc). I'm just not sure if I should be pushing image data around between pages in the way I'm proposing. I'm starting to think I should save the image to DB in View1 instead, although I would need a housekeeping process to periodically delete images in scenarios where the user never actually clicked "save" on View2. – Andrew Stephens Jul 31 '15 at 09:07
  • You are correct. TempData should be the one for redirect. My mistake. For your second question, it really depends on your design and business scenarios. If you need to store the file uploading process and allow the user to come back and finish, then persist the images in DB and purge them regularly is probably a good idea. – Shawn Song Aug 03 '15 at 02:23