改太多
This commit is contained in:
1 parent
be5352efed
commit
1cb2a7a35e
48 files changed
+17970
-485
No files matched your search
@@ -14,11 +14,13 @@ using System.Data.Entity;
|
||||
using System.Data.Entity.Core.Objects;
|
||||
using System.Diagnostics;
|
||||
using System.IdentityModel.Metadata;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using System.Web.Helpers;
|
||||
using System.Web.Http;
|
||||
using static TreeView;
|
||||
@@ -211,6 +213,69 @@ public class orderController : ApiController
|
||||
return count;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/order/import")]
|
||||
public IHttpActionResult import() {
|
||||
|
||||
// 1. 檢查請求是否為 multipart/form-data
|
||||
if (!Request.Content.IsMimeMultipartContent())
|
||||
{
|
||||
return Content(HttpStatusCode.UnsupportedMediaType, "不支援的媒體類型,必須是表單資料。");
|
||||
}
|
||||
|
||||
// 2. 定義伺服器上的暫存/儲存資料夾路徑
|
||||
var rootUrl = HttpContext.Current.Server.MapPath("~/upload/temp");
|
||||
|
||||
// 確保資料夾存在
|
||||
if (!Directory.Exists(rootUrl))
|
||||
{
|
||||
Directory.CreateDirectory(rootUrl);
|
||||
}
|
||||
|
||||
// 3. 使用 StreamProvider 來自動接收並寫入檔案
|
||||
var provider = new MultipartFormDataStreamProvider(rootUrl);
|
||||
|
||||
try
|
||||
{
|
||||
// 非同步讀取表單內容,檔案會在此時被自動寫入 rootUrl 資料夾(檔名會變成 BodyPart_xxx 暫存名稱)
|
||||
Request.Content.ReadAsMultipartAsync(provider);
|
||||
|
||||
// 4. 取得前端傳過來的其他純文字欄位 (例如:formData.append('userId', '123'))
|
||||
string act_num = provider.FormData["act_num"];
|
||||
|
||||
// 5. 處理檔案(將暫存檔更名為原本的檔案名稱)
|
||||
foreach (var fileData in provider.FileData)
|
||||
{
|
||||
// 取得原始檔名(通常帶有雙引號,如 "photo.jpg",需要去除)
|
||||
string originalFileName = fileData.Headers.ContentDisposition.FileName.Trim('"');
|
||||
|
||||
// 取得當前寫入的暫存路徑
|
||||
string localFileName = fileData.LocalFileName;
|
||||
|
||||
// 組合最終要儲存的完整路徑
|
||||
string finalFilePath = Path.Combine(rootUrl, originalFileName);
|
||||
|
||||
// 如果已有同名檔案,先刪除或自行加上序號、GUID 防止覆蓋
|
||||
if (File.Exists(finalFilePath))
|
||||
{
|
||||
File.Delete(finalFilePath);
|
||||
}
|
||||
|
||||
// 將暫存檔重新命名/移動到最終目的地
|
||||
File.Move(localFileName, finalFilePath);
|
||||
}
|
||||
//讀取內容
|
||||
|
||||
|
||||
return Ok(new { message = "檔案上傳成功!" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return InternalServerError(ex);
|
||||
}
|
||||
return Ok(new { result = "Y", message = "完成" });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/order/BatchSave")]
|
||||
public IHttpActionResult BatchSave([FromBody] Model.ViewModel.pro_order po)
|
||||
|
||||
Reference in new issue
Block a user