protected void ButtonUpload_Click(object sender, EventArgs e) { if (FileUploadInput.HasFile) { var uploadDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InputFiles"); if (!Directory.Exists(uploadDir)) { Directory.CreateDirectory(uploadDir); } var uploadFilePath = Path.Combine(uploadDir, FileUploadInput.FileName); //Clear Exisiting File. if (File.Exists(uploadFilePath)) { File.Delete(uploadFilePath); } FileUploadInput.SaveAs(uploadFilePath); TextBoxInput.Text = File.ReadAllText(uploadFilePath); File.Delete(uploadFilePath); } }
public async Task <IActionResult> UploadPackage([FromForm] FileUploadInput input) { if (Request.Form.Files == null || Request.Form.Files.Count == 0) { return(BadRequest(ApiResult.Failed("未上传有效文件"))); } if (Request.Form.Files.Count > _uploadOption.MaxUpload) { return(BadRequest(ApiResult.Failed($"超过单次上传数量限制:{_uploadOption.MaxUpload}"))); } if (string.IsNullOrWhiteSpace(input.Extension) && Request.Form.Files.Any(x => string.IsNullOrWhiteSpace(x.FileName) || string.IsNullOrWhiteSpace(Path.GetExtension(x.FileName)))) { return(BadRequest(ApiResult.Failed("请手动设置文件扩展名"))); } return(await DecompressionUpload(input)); }
public async Task <IActionResult> Upload([FromForm] FileUploadInput input) { if (Request.Form.Files == null || Request.Form.Files.Count == 0) { return(BadRequest(ApiResult.Failed("未上传有效文件"))); } if (Request.Form.Files.Count > _uploadOption.MaxUpload) { return(BadRequest(ApiResult.Failed($"超过单次上传数量限制:{_uploadOption.MaxUpload}"))); } if (string.IsNullOrWhiteSpace(input.Extension) && Request.Form.Files.Any(x => string.IsNullOrWhiteSpace(x.FileName) || string.IsNullOrWhiteSpace(Path.GetExtension(x.FileName)))) { return(BadRequest(ApiResult.Failed("请手动设置文件扩展名"))); } var tasks = Request.Form.Files .Select(file => { string ext = null; if (!string.IsNullOrWhiteSpace(file.FileName)) { ext = Path.GetExtension(file.FileName); } var model = new UploadFileModel(file.OpenReadStream(), ext ?? input.Extension, input.Group); return(_fileStorageService.UploadAsync(model)); }) .ToArray(); await Task.WhenAll(tasks); return(Json(new ApiResult <List <string> >(tasks.Select(x => x.Result).ToList()))); }
/// <summary> /// 解压文件并上传 /// </summary> /// <param name="input"></param> /// <returns></returns> private async Task <IActionResult> DecompressionUpload(FileUploadInput input) { string tempDir = null; try { tempDir = FilePathExtention.CreateTimeRandomDir(Path.GetTempPath()); var lstFile = await new CompressTool(tempDir).Decompression(Request.Form.Files); var tasks = lstFile .Select(async file => { var ext = Path.GetExtension(file.FileName); using (var stream = System.IO.File.OpenRead(file.TempFilePath)) { var model = new UploadFileModel(stream, ext, input.Group); file.FileUrl = await _fileStorageService.UploadAsync(model); } //上传成功,删除临时文件 System.IO.File.Delete(file.FileName); }).ToArray(); await Task.WhenAll(tasks); return(Json(new ApiResult <List <CompressFileUploadOutput> >(lstFile))); } catch (FormatException ex) { return(Json(new ApiResult <string>(ex.Message))); } finally { if (tempDir != null && Directory.Exists(tempDir)) { Directory.Delete(tempDir, true); } } }