Reputation: 14417
I have this code:
[HttpPost]
public ActionResult Edit(Photograph photo, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
if (image != null)
{
byte[] imageData = new byte[image.ContentLength];
image.InputStream.Read(imageData, 0, image.ContentLength);
System.IO.File.WriteAllBytes(HttpContext.Server.MapPath("~/Content/Images"), imageData);
}
repository.SavePhotograph(photo);
TempData["message"] = string.Format("{0} has been saved", photo.Name);
return RedirectToAction("Index");
}
else
{
return View(photo);
}
}
It is meant to take the photo uploaded from a form and save it in the server (local server). I've given the folder "Visual Studio 2010" where all the projects are kept NETWORK SERVICE user permissions and IIS_IUSRS group permissions. I'm also running visual studio as administrator.
For some reason it still won't let me save the file to that location. The location is within the project folder under {Project_Name\Content\Images}. How do I get it to save the file there?
Thanks,
Upvotes: 1
Views: 1301
Reputation: 14417
The reason it was giving me that error is because the method:
System.IO.File.WriteAllBytes()
Takes, the path of the FILE and the data.
I was passing a folder
System.IO.File.WriteAllBytes(HttpContext.Server.MapPath("~/Content/Images"), imageData);
I should have been passing a file:
System.IO.File.WriteAllBytes(String.Format("{0}{1}", HttpContext.Server.MapPath("~/Content/Images"), image.FileName), imageData);
Now I'm getting an error with my repository so I'm looking into that :) I love coding so much .... ;)
Upvotes: 1
Reputation: 47804
For some reason it still won't let me save the file to that location.
Are you getting any errors ? if yes please post full errors here.
Case I : If you are using Visual Studio to run your MVC app, make sure you run visual studio using the 'Run as Administrator' Option.
Case II: If you are running your MVC app on IIS. Goto the folder where the images are being saved. That is grant 'IIS_IUSRS' user Full control 'modify' rights.
Upvotes: 0
Reputation: 1561
RE: the line with "System.IO.File.WriteAllBytes"... are you trying to save to the directory? Where does it selected a filename for the uploaded file?
Upvotes: 0