public async Task <ActionResult> UploadFiles([FromForm] UploadRequest vm) { var file = vm.File; if (file == null || file.Length > _maxFileUplodSize) { return(BadRequest()); } try { var userId = vm.UserId; var fileName = Path.GetFileName(file.FileName); var storageDayPath = Path.Combine(this.StorageDirectoryPath, DateTime.Now.ToString("yyyy-MM-dd"), userId.ToString()); if (!Directory.Exists(storageDayPath)) { Directory.CreateDirectory(storageDayPath); } //var unfinishedFiles = Directory.GetFiles(storageDayPath) // .Where(fn => Path.GetFileName(fn).StartsWith("_current_")) // .ToList() // ; //if (unfinishedFiles.Any()) //{ // unfinishedFiles.ForEach(System.IO.File.Delete); //} var storeFullPath = Path.Combine(storageDayPath, $"{DateTime.Now.Ticks}_{fileName}"); using (var fw = new FileStream(storeFullPath, FileMode.Create)) { file.CopyTo(fw); fw.Flush(); fw.Close(); } var history = new UploadHistoryModel(); history.UserId = userId; history.FileName = fileName; history.SwitchId = vm.SwitchId; history.DateUpload = DateTime.UtcNow; history.Step = ImportStep.FormatControl; this.TPMContext.UploadHistory.Add(history); await this.TPMContext.SaveChangesAsync(); var command = new ProccessFileCommand { UserId = userId, ImportHistoryId = history.Id, FilePath = storeFullPath }; this.FileCommandQueue.Enqueue(command); return(Ok(new { Status = "Ok", HistoryId = history.Id })); } catch (Exception ex) { this.Logger.LogError(ex, "Error on upload file"); return(StatusCode(500, "Unexpected error")); } }
static int maxsize = 3 * 1024 * 1000;//3mb public void ProcessRequest(HttpContext context) { context.Response.AddHeader("Access-Control-Allow-Origin", "*"); context.Response.ContentType = "application/json"; ResultImages result = new ResultImages(); try { string json = context.Request.QueryString["token"]; json = Encryptor.DecryptString(Encryptor.Base64Decode(json), KEY); if (!string.IsNullOrEmpty(json)) { var data = JsonConvert.DeserializeObject <TokenUpload>(json); if (context.Request.Files.Count > 0) { HttpPostedFile file = null; for (int i = 0; i < context.Request.Files.Count; i++) { file = context.Request.Files[i]; Logs.SaveLog("File info: " + file.FileName + ", " + file.ContentLength); if (file.ContentLength > 0 && file.ContentLength < maxsize) { #region process upload images EditImages objUpload = new EditImages(); string url = "upload/" + data.publisherName + "/" + DateTime.Now.ToString("yyyy/MM/dd").Replace('/', '_') + "/"; string path = context.Server.MapPath(url); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string typeIMG = System.IO.Path.GetExtension(file.FileName); string fileName = GetUniqueFileName(file.FileName, path, typeIMG); objUpload.sFileName = fileName; objUpload.iMaxWebWidth = sizeWeb; objUpload.iMaxTabletWidth = sizeTable; objUpload.iMaxPhoneWidth = sizePhone; objUpload.sPath = path; objUpload.fileUpped = file; objUpload.Process_Upload_v2(); picture pic = new picture(); pic.fileName = objUpload.sFileName + typeIMG; pic.urlWeb = context.Request.Url.GetLeftPart(UriPartial.Authority) + "/" + url + pic.fileName; pic.urlTablet = context.Request.Url.GetLeftPart(UriPartial.Authority) + "/" + url + "ThumbTable_" + pic.fileName; pic.urlPhone = context.Request.Url.GetLeftPart(UriPartial.Authority) + "/" + url + "ThumbPhone_" + pic.fileName; pic.sizeWeb = calculatorSizeFile(objUpload.sizeWeb); pic.sizeTablet = calculatorSizeFile(objUpload.sizeTablet); pic.sizePhone = calculatorSizeFile(objUpload.sizePhone); pic.sizeOld = calculatorSizeFile(file.ContentLength); pic.ip = data.clientIp; pic.userId = int.Parse(data.userId); pic.serverTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"); UploadHistoryModel u = new UploadHistoryModel(); u.AddUploadHistory(pic); if (pic.id > 0) { result.status = NUMBER_CODE.SUCCESS; result.message = result.status.ToString(); result.data = pic; } else { result.status = NUMBER_CODE.ERROR_ADDHISTORY; result.message = "Upload file không thành công!"; } #endregion } else { result.status = NUMBER_CODE.MAXMaximum_request_length_exceeded; result.message = "File upload không được vượt quá 3MB!"; } } } else { result.status = NUMBER_CODE.NO_FILE; result.message = NUMBER_CODE.NO_FILE.ToString(); } } else { result.status = NUMBER_CODE.TOKEN_WRONG; result.message = result.status.ToString(); } } catch (Exception ex) { Logs.SaveError("ERROR UploadPicture", ex, true); result.status = NUMBER_CODE.EX; result.message = ex.ToString(); } context.Response.Write(JsonConvert.SerializeObject(result)); }