0

I followed solution from this question Preview an image before it is uploaded to show preview of images users want to submit to my website.

I have this code that shows image preview on page

<input type="file" accept="image/*" onchange="loadFile(event)">
<img id="output"/>
<script>
  var loadFile = function(event) {
    var output = document.getElementById('output');
    output.src = URL.createObjectURL(event.target.files[0]);
  };
</script>

and form with some additional data.

My question is, how to save image to a /uploads/ folder of my webapp when user click on submit form button? I don't need to save image in database, but on web server in folder.

Community
  • 1
  • 1
onedevteam.com
  • 3,215
  • 10
  • 34
  • 67
  • [Uploading a File With ASP.NET MVC](http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/) –  Oct 19 '15 at 23:19

1 Answers1

0

In the POST action of your controller, you may access the InputStream in the Request object and then copy the InputStream of the Posted File to a File Stream. Here's a sample code.

foreach (string item in Request.Files)
{
    HttpPostedFileBase file = Request.Files[item];
    string imagePath = Path.Combine(Server.MapPath("~/Uploads"), file.FileName);
    using(FileStream fs = File.Create(imagePath)
    {
        file.InputStream.CopyTo(fs);
    }
}
Bon Macalindong
  • 1,208
  • 12
  • 18