如何显示已在数据库中上传的pdf和doc文件的缩略图

时间:2017-05-17 06:05:11

标签: c# asp.net asp.net-mvc-4

我有一种情况,我上传数据库中的每种类型的文件,并在视图中显示相关的图标,但我想显示每个文件的缩略图,因为我正在显示图像文件的图像。

请建议我如何显示doc,pdf,xlxs或任何文件格式的缩略图。

以下是我上传文件的代码

 [HttpPost]
    public ActionResult UploadResources(HttpPostedFileBase[] files)
    {
        if (ModelState.IsValid && Request.Form["SelectedSubCategoryId"] != null && Request.Form["SelectedCategoryId"] != null)
        {
            string result = string.Empty;
            if (files != null && files.Length > 0 && files[0] !=null )
            {
                string[] results = new string[files.Length];
                int i = 0;
                foreach (HttpPostedFileBase file in files)
                {
                    string ext = string.Empty;
                    string fileName = file.FileName;
                    int fileExtPos = fileName.LastIndexOf(".");
                    if (fileExtPos >= 0)
                        ext = fileName.Substring(fileExtPos, (fileName.Length - fileExtPos));

                    results[i] = dat.UploadResources(Convert.ToInt32(Request.Form["SelectedCategoryId"]), Convert.ToInt32(Request.Form["SelectedSubCategoryId"]), Convert.ToString(Request.Form["Description"]), WebSecurity.CurrentUserId, Convert.ToBoolean(Request.Form["IsDisplay"]), Convert.ToString(Request.Form["YoutubeURL"]), file.FileName, ext, file.InputStream);
                    i++;
                }
                if (results.Any(m => m == "1"))
                {
                    string Upfilenames = string.Empty;
                    string Npfilenames = string.Empty;
                    int m = 0;
                    foreach (string s in results)
                    {
                        if (s == "1")
                        {
                            Upfilenames += files[m].FileName + ",";
                        }
                        else
                        {
                            Npfilenames += files[m].FileName + ",";
                        }
                        m++;
                    }
                   TempData["Message"] = "Success:" + Upfilenames + "were uploaded.";
                }
                if (results.Any(m => m == "-2"))
                {
                    TempData["Message"] = "Danger: File not uploaded, Please try again.";
                }
                //else
                //{
                //    TempData["Message"] = "Success: All files were uploaded.";
                //}
            }
            else if (Request.Form["YoutubeURL"] != null)
            {
                result = dat.UploadResources(Convert.ToInt32(Request.Form["SelectedCategoryId"]), Convert.ToInt32(Request.Form["SelectedSubCategoryId"]), Convert.ToString(Request.Form["Description"]), WebSecurity.CurrentUserId, Convert.ToBoolean(Request.Form["IsDisplay"]), Convert.ToString(Request.Form["YoutubeURL"]), null, null, null);
                if (result == "1")
                {
                    TempData["Message"] = "Success: Resource uploaded.";
                }

                else if (result == "-2")
                {
                    TempData["Message"] = "Danger: Resource not uploaded, Please try again.";
                }
            }

        }
        return RedirectToAction("UploadResources");
    }

也是这个

 #region UploadResources

    public string UploadResources(int CategoryId, int SubCategoryId, string Description, int AdminId,bool IsDisplay ,string YoutubeURL = null,string FileName = null, string fileExt = null, Stream Media = null)
    {
        string RetValue = string.Empty;

        string ProcedureName =string.Empty;
        string ReturnValue = string.Empty;
        string regex = "^(?:https?\\:\\/\\/)?(?:www\\.)?(?:youtu\\.be\\/|youtube\\.com\\/(?:embed\\/|v\\/|watch\\?v\\=))([\\w-]{10,12})(?:$|\\&|\\?\\#).*";
        string YoutubeID = string.Empty;
        if(YoutubeURL != null)
        {   
            YoutubeID = new Regex(regex).Match(YoutubeURL).Groups[1].Value;
        }
        ProcedureName = "BP_UPLOAD_RESOURCE";
        using (con = new SqlConnection(constrBrandpier))
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(ProcedureName, con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@CATEGORY_ID", CategoryId);
                cmd.Parameters.AddWithValue("@SUB_CATEGORY_ID", SubCategoryId);
                cmd.Parameters.AddWithValue("@FILENAME", FileName);
                cmd.Parameters.AddWithValue("@FILEEXT", fileExt);
                cmd.Parameters.AddWithValue("@DESCRIPTION", Description);
                cmd.Parameters.AddWithValue("@ADMINID", AdminId);
                cmd.Parameters.AddWithValue("@YOUTUBE_URL", YoutubeURL);
                cmd.Parameters.AddWithValue("@YOUTUBE_ID", YoutubeID);
                cmd.Parameters.Add(new SqlParameter("@ISDISPLAY", SqlDbType.Char)).Value = IsDisplay ? '1' : '0';

                SqlParameter IMAGEID = new SqlParameter();
                IMAGEID = cmd.Parameters.Add("@IMAGEID", SqlDbType.NVarChar, 300);
                IMAGEID.Direction = ParameterDirection.Output;

                SqlParameter intReturn = new SqlParameter();
                intReturn = cmd.Parameters.Add("@INTRETURN", SqlDbType.Int);
                intReturn.Direction = ParameterDirection.Output;

                cmd.ExecuteScalar();
                RetValue = intReturn.Value.ToString();

                if (intReturn.Value.ToString() == "1" && FileName != null)
                {
                    string _bucketpath = ConfigurationManager.AppSettings["bucketName"].ToString() + ConfigurationManager.AppSettings["ResourcebucketName"].ToString() + CategoryId.ToString() + "/" + SubCategoryId.ToString();
                    BWS.AWS.S3 s3 = new BWS.AWS.S3();
                    string _returns3 = s3.UploadFile(_bucketpath, IMAGEID.Value.ToString(), ReadFully(Media));
                }
            }
            catch (Exception ex)
            {
                RetValue = ex.Message.ToString();
            }
        }
        return RetValue;

    }

1 个答案:

答案 0 :(得分:0)

对于上传的图片,您可以使用Twitter的引导缩略图类,如下所示。

<img src="<path-to-uploaded image>" class="img-thumbnail" alt="This is a thumbnail image" width="150" height="100">

您可以在https://www.w3schools.com/bootstrap/bootstrap_images.asp

查看更多内容

对于PDF图标,我只使用带有PDF徽标的标准图像并使用它。

我的两分钱。