Uploading/Saving videos to database or file system

痞子三分冷 提交于 2019-12-12 13:54:20

问题


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");
}

回答1:


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:

  • Less overhead. It's quicker to load via the filesystem than via a connection to a (potentially non-local) database.
  • Large object allocations are minimal: A large object is already created during the upload. If you were to convert to a byte array you're creating yet another large object in memory.

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.



来源:https://stackoverflow.com/questions/13792856/uploading-saving-videos-to-database-or-file-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!