public async Task <ActionResult> FileUploadPost([FromForm] CreateFileUploadResource NewFile) // 新增上傳檔案 { // 判斷欄位是否正確 if (!ModelState.IsValid) { return(BadRequest(new { message = "資料填寫錯誤" })); } // 判斷是否有上傳檔案 if (NewFile.UploadFile != null) { // 呼叫至Services 進行寫入資料庫 var InsertResult = await _FileUploadService.FileUpload(NewFile); if (InsertResult.Equals("新增成功")) { // 回傳OK return(Ok(new { message = "上傳成功" })); } else if (InsertResult.Equals("非指定檔格式")) { return(BadRequest(new { message = "非指定檔格式" })); } else if (InsertResult.Equals("資料庫寫入錯誤")) { return(BadRequest(new { message = "寫入資料庫失敗" })); } } return(BadRequest(new { message = "無上傳檔案" })); }
public async Task <IHttpActionResult> UploadImages() { string fileUrls = ""; var image = new Image(); var context = HttpContext.Current; var root = context.Server.MapPath("~/bin"); var provider = new MultipartFormDataStreamProvider(root); try { await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.FileData) { var url = await _fileUploadService.FileUpload(file); fileUrls += url; if (provider.FileData.Count > 1) { fileUrls += ";"; } } image.Urls = fileUrls; return(Ok(image)); } catch (Exception e) { return(BadRequest(e.Message)); } }