如何在SQL Server 2014中存储和检索文件名

时间:2016-04-19 11:05:59

标签: asp.net-mvc-4 sql-server-2014

我上传了3个文件,即视频,图片和word文件。现在我想从数据库中存储和检索它,最终视频名称保存在数据库中,但图像名称和单词文件名不存储也不检索。我该怎么办?

    [HttpPost]
    public ActionResult AddSKU(SKU_Det skufiles, IEnumerable<HttpPostedFileBase> files)
    {
        TraderCartMvcEntities db = new TraderCartMvcEntities();

        //UploadLogo(skufiles.File);
        var httpPostedFile = Request.Files[0];

        if (httpPostedFile != null)
        {
            // Validate the uploaded file if you want like content length(optional)
            // Get the complete file path
            var uploadFilesDir = System.Web.HttpContext.Current.Server.MapPath("~/Content/Videos");

            if (!Directory.Exists(uploadFilesDir))
            {
                Directory.CreateDirectory(uploadFilesDir);
            }

            var fileSavePath = Path.Combine(uploadFilesDir, httpPostedFile.FileName);
            httpPostedFile.SaveAs(fileSavePath);

            foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Areas/Admin/Images/") + file.FileName);
                }
            }

            //if (file != null)
            //{
            //    file.SaveAs(HttpContext.Server.MapPath("~/Areas/Admin/Images/")
            //                                          + file.FileName);
            //}

            SKU_Det sku = new SKU_Det();

            sku.SKU = skufiles.SKU;
            sku.VideoPath = httpPostedFile.FileName;
            sku.Imagepath = skufiles.SKU;
            sku.FilePath = skufiles.SKU;
            db.SKU_Det.Add(sku);
            db.SaveChanges();
        }

        return RedirectToAction("Index");
    }

0 个答案:

没有答案