Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a situation where i am uploading every type of file in the database and displaying related icons in the view, But i want to display Thumbnail of each file as i am displaying the image for the image file.

please suggest me how I'll show a thumbnail for doc, pdf, xlsx, or any file format.

What I have tried:

Here is method which i am using to upload every file.
[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");
        }


and

#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;

       }
Posted
Updated 16-May-17 20:00pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900