예제 #1
0
        public ActionResult UploadFile(HttpPostedFileBase file, int id, int type, int loginId)
        {
            if ((file == null) || (file.ContentLength <= 0))
            {
                return(Json(new { result = false, message = "File does not exist." }));
            }
            var fileName     = Path.GetFileName(file.FileName);
            var validateFile = type == (int)DocumentType.StockPicture ? _systemService.CheckValidPictureExtension(fileName) : _systemService.CheckValidExtension(fileName);

            if (!validateFile)
            {
                const string messageErrorPicture  = "Invalid file. Unfortunately it was not possible to upload the file because it is not supported by our system. We are able to accept documents in the following formats: .jpg; .jpeg; .gif; .png.";
                const string messageErrorDocument = "Invalid file. Unfortunately it was not possible to upload the file because it is not supported by our system. We are able to accept documents in the following formats: .txt; .doc; .xls; .csv; .pdf; .docx; .xlsx; .jpg; .jpeg; .gif; .png.";
                var          message = type == (int)DocumentType.StockPicture ? messageErrorPicture : messageErrorDocument;
                return(Json(new
                {
                    result = false,
                    message
                }));
            }
            // check file size
            var maxPictureSize  = Convert.ToInt64(WebConfigurationManager.AppSettings["PictureSize"]);
            var maxDocumentSize = Convert.ToInt64(WebConfigurationManager.AppSettings["DocumentSize"]);
            var maxFile         = type == (int)DocumentType.StockPicture ? maxPictureSize : maxDocumentSize;

            if (file.ContentLength > maxFile * 1024)
            {
                return(Json(new { result = false, message = "File size is more than " + maxFile + " bytes (" + maxFile / 1024 + " Mb), cannot be uploaded" }));
            }

            var fileExtend   = Path.GetExtension(fileName);
            var nameUrl      = Path.GetFileNameWithoutExtension(fileName) + "_" + id + "_" + DateTime.Now.ToString("ddMMyyyyHHmmss");
            var documentUrl  = nameUrl + fileExtend;
            var saveLocation = type == (int)DocumentType.StockPicture ? WebConfigurationManager.AppSettings["PathImg"] : WebConfigurationManager.AppSettings["PathDoc"];
            var fullFilePath = _systemService.GetDocumentUrl(saveLocation, nameUrl, fileExtend);

            try
            {
                file.SaveAs(Server.MapPath(fullFilePath));
            }
            catch (Exception e)
            {
                return(Json(new { result = false, message = e.Message }));
            }

            // Save to db
            var document      = _systemService.AddDocument(documentUrl, string.Empty, id, type, fileName, string.Empty, saveLocation, new byte(), loginId);
            var fileViewModel = new FileViewModel()
            {
                FileId   = document.Id,
                FileGuid = documentUrl,
                FileName = fileName,
                //FileSize = Math.Round((float)file.ContentLength / 1048576, 2).ToString(),
                FileSource = fullFilePath,
                ActionDate = DateTime.Now.ToString("MM/dd/yyyy")
            };

            return(Json(new { result = true, file = fileViewModel }));
        }