Esempio n. 1
0
        public ActionResult SetImage(int id, FileModel photo)
        {
            try
            {
                photo.ProductID = id;

                if (Request.IsAjaxRequest())
                {
                    photo.Data        = Request.BinaryRead(Request.ContentLength);
                    photo.Name        = Request.Headers.Get("X-File-Name");
                    photo.ContentType = Request.ContentType;

                    string error = photo.Validate;
                    if (error == null)
                    {
                        files.AddFile(photo);
                        return(Json(new { success = true }));
                    }

                    return(Json(new { errors = error }));
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        photo.Name        = photo.file.FileName;
                        photo.ContentType = photo.file.ContentType;
                        byte[] tmp = new byte[photo.file.ContentLength];
                        photo.file.InputStream.Read(tmp, 0, photo.file.ContentLength);
                        photo.Data = tmp;

                        files.AddFile(photo);
                        return(RedirectToAction("Details", "Product", new { id = id }));
                    }

                    return(View(photo));
                }
            }
            catch (Exception e)
            {
                if (Request.IsAjaxRequest())
                {
                    return(Json(new { success = false, error = e.Message }));
                }
                else
                {
                    ViewData["ERROR"] = e.Message;
                    return(View());
                }
            }
        }
Esempio n. 2
0
        public ActionResult UploadFile(int orderID)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Json("Error"));
            }
            var id     = (FormsIdentity)User.Identity;
            var ticket = id.Ticket;

            try
            {
                HttpPostedFileBase hpf           = HttpContext.Request.Files["file"] as HttpPostedFileBase;
                DirectoryInfo      di            = Directory.CreateDirectory(Server.MapPath("~/Tmp/Files"));// If you don't have the folder yet, you need to create.
                string             savedFileName = Path.Combine(di.FullName, hpf.FileName);
                hpf.SaveAs(savedFileName);
                fileMgr.AddFile(new Image {
                    FileName = savedFileName, OrderID = orderID
                });
                var msg = new { msg = "File Uploaded", filename = hpf.FileName, url = savedFileName };

                return(Json(msg));
            }
            catch (Exception e)
            {
                //If you want this working with a custom error you need to change in file jquery.uploadfile.js, the name of
                //variable customErrorKeyStr in line 85, from jquery-upload-file-error to jquery_upload_file_error
                var msg = new { jquery_upload_file_error = e.Message };
                return(Json(msg));
            }
        }