上传/保存视频到数据库或文件系统 [英] Uploading/Saving videos to database or file system

查看:700
本文介绍了上传/保存视频到数据库或文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有试图挽救一个视频之前,所以我不很了解这一点。我知道,如果视频是小,我可以转换为字节数组,保存到数据库,但更有效率,我想学习如何将任何上传的视频(S)保存到我的服务器上的文件,然后只需保存的文件路径视频在我的数​​据库表中。我完全不知道如何开始这样做。我见过以下,但也不太清楚它在做什么这个例子。对我来说,它出现的任何保存上传的视频,任何用户上传到App_Data文件夹。是对的吗?任何帮助是极大的AP preciated。

  [HttpPost]
公共ContentResult类型UploadFiles()
{
       变种R =新的List< ViewDataUploadFilesResult>();       的foreach(在Request.Files字符串的文件)
       {
        HttpPostedFileBase HPF = Request.Files [文件]作为HttpPostedFileBase;
        如果(hpf.ContentLength == 0)
        继续;    字符串savedFileName = Path.Combine(使用Server.Mappath(〜/ App_Data文件),Path.GetFileName(hpf.FileName));
    hpf.SaveAs(savedFileName); //保存文件    r.Add(新ViewDataUploadFilesResult()
    {
        名称= hpf.FileName,
        长度= hpf.ContentLength,
        TYPE = hpf.ContentType
    });
}
//返回JSON
返回的内容({\\名称\\:\\+ R [0] .Name点+\\,\\式\\:\\+ R [0]。键入+\\,\\大小\\:\\+的String.Format({0}字节,R [0]。长度)+\\},应用/ JSON);
}


解决方案

您的假设是正确的..它也返回一个JSON字符串与名称,内容类型和长度。这无疑是一个异步上传

它通常是更好地将文件存储在磁盘上,而不是使用一个数据库。一对夫妇的理由是:


  • 少开销。这是更快的通过文件系统加载到通过一个(潜在的非本地)数据库的连接比。

  • 分配大型对象是最小的:在上载过程中已经创建一个大对象。如果你要转换成你在内存中创建的又一大对象的字节数组。

您可能需要使用数据库的原因之一是定制版本或保存文件的正确轨道(通过额外的元数据..等)。然而,这一般可以使用的文件系统也做

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:

  • 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.

这篇关于上传/保存视频到数据库或文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆