Reputation: 3397
Building web app in asp mvc3 with razor and sql server 2012.
I am stepping into new territory and my research has yet to come up with an answer. I am building a site that allows users to create a profile and upload an image. Of course I hope to have a ton of users so I am saving the images to file server, with a link in the db. However I have never built such a thing and I am trying to figure out the 'app to file server' relationship, specifically the path to the image location. Do I just create a file in my project folder and store them there? Or do I need to put in a temp path, then get the actual path from my host after deploy?
Currently i am storing in my solution:
[HttpPost]
public ActionResult ImageUpload(HttpPostedFileBase file)
{
string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.ImageUploadMessage = "File Uploaded Successfully";
return View();
}
This code works fine, assuming I have a folder in the root called 'Images', but this does not seem correct for a live site.
Upvotes: 0
Views: 1467
Reputation: 2597
Rather creating a directory in your application you can also create a virtual directory on server and give its path to upload the file like this:
string path = "/VirtualDirecotryName/";
path = Path.Combine(Server.MapPath(path), fileName);
fileupload.SaveAs(path);
and also store the reference to the file in database. This is how I am managing user's profile pic in my application because it is very easy for me to maintain as files are being uploading on separate virtual directory not in application. Hope this will help you.
Upvotes: 2