Пример #1
0
        public ActionResult UploadFile()
        {
            MessageJS returnmodel = new MessageJS();

            if (Request.Files.Count > 0 && Request.Files[0] != null)
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var httpPostedFile = Request.Files[i];

                    string[] strSplit = httpPostedFile.FileName.Split('.');

                    string filetype = strSplit[strSplit.Length - 1].ToLower();

                    if (httpPostedFile.FileName != "" && (filetype == "jpg" || filetype == "jpeg" || filetype == "png"))
                    {
                        string srvPath = Server.MapPath("../Files/img");
                        string path    = "/Files/img/" + httpPostedFile.FileName;

                        string savingpath = "";
                        try
                        {
                            using (MD5 md5Hash = MD5.Create())
                            {
                                string hash = HashMedia.GetMd5Hash(md5Hash, httpPostedFile.FileName + "" + DateTime.Now.ToString());

                                savingpath = srvPath + "/" + hash + "." + filetype;
                                httpPostedFile.SaveAs(savingpath);
                                savingpath = "/Files/img/" + hash + "." + filetype;

                                MEDIAT upload = new MEDIAT();
                                upload.Created    = DateTime.Now;
                                upload.Emri       = Guid.NewGuid();
                                Session["Emri"]   = upload.Emri;
                                upload.Extensioni = (int)Enums.FileType.image;
                                upload.Pershkrimi = httpPostedFile.FileName;
                                //upload.Tipi = (int)FileType.image;
                                upload.Shtegu = savingpath;
                                Session["UploadedPicture"] = upload;
                                returnmodel.Status         = true;
                                returnmodel.Message        = "Imazhi u ngarkua me sukses!";
                            }
                        }
                        catch (Exception ex)
                        {
                            returnmodel.Status  = false;
                            returnmodel.Message = "Ka ndodhur nje gabim!";
                            //throw (ex);
                            //return false;
                        }
                    }
                    else
                    {
                        returnmodel.Status  = false;
                        returnmodel.Message = "File nuk eshte ne formatin e duhur!";
                    }
                }
            }
            return(Json(returnmodel, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        public async Task <bool> UploadFileAsync(MEDIAT model)
        {
            Session["UploadedPicture"] = null;
            var media = await db.MEDIATs.FindAsync(model.ID);

            // Get the uploaded image from the Files collection
            string savingpath = "";

            try
            {
                if (Request.Files.Count > 0 && Request.Files[0] != null)
                {
                    var httpPostedFile = Request.Files["UploadedImage"];
                    if (httpPostedFile.FileName != "")
                    {
                        string srvPath = Server.MapPath("../Files/img");
                        string path    = "/Files/img/" + httpPostedFile.FileName;

                        using (MD5 md5Hash = MD5.Create())
                        {
                            string hash = HashMedia.GetMd5Hash(md5Hash, httpPostedFile.FileName + "" + DateTime.Now.ToString());

                            string[] strSplit = httpPostedFile.FileName.Split('.');

                            string filetype = strSplit[strSplit.Length - 1];
                            if (httpPostedFile.FileName != "" && (filetype == "jpg" || filetype == "jpeg" || filetype == "png"))
                            {
                                savingpath = srvPath + "/" + hash + "." + filetype;
                                httpPostedFile.SaveAs(savingpath);
                                savingpath = "/files/img/" + hash + "." + filetype;

                                MEDIAT upload = new MEDIAT();

                                upload.Created    = DateTime.Now;
                                upload.Createdby  = 1;
                                upload.Pershkrimi = httpPostedFile.FileName;
                                if (model.Pershkrimi != null)
                                {
                                    upload.Pershkrimi = model.Pershkrimi;
                                }
                                upload.Shtegu     = savingpath;
                                upload.Emri       = Guid.NewGuid();
                                upload.Extensioni = (int)Enums.FileType.image;
                                db.MEDIATs.Add(upload);
                                await db.SaveChangesAsync();

                                Session["UploadedPicture"] = upload;
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }