I have never tried to save a video before so I don’t know much about this. I know if the video is small I can convert to byte array and save to db, however to be more efficient I would like to learn how to save any uploaded video(s) to my servers file and then just save the file path of the video in my database table. I have absolutely no idea how to begin to do this. I’ve seen this example below but not too sure what its doing. To me it appears it’s saving any uploaded videos that any user uploads to the App_Data folder. Is that right? Any help is greatly appreciated.
[HttpPost]
public ContentResult UploadFiles()
{
var r = new List<ViewDataUploadFilesResult>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName); // Save the file
r.Add(new ViewDataUploadFilesResult()
{
Name = hpf.FileName,
Length = hpf.ContentLength,
Type = hpf.ContentType
});
}
// Returns json
return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json");
}
Your assumption is correct.. it is also returning a JSON string with the name, content type and length. This is no doubt an asynchronous upload.
It is generally better to store the files on disk rather than use a database. A couple of reasons are:
One reason you might want to use a database is for custom versioning or keeping proper track of files (via extra metadata.. etc). However, this can generally be done using a filesystem also.