雖然還有其他類似的帖子,但它們實際上都不是專注于使用 Javascript PageMethod 檔案上傳的 ASP.NET 網站。我想從我的檔案選擇器中選擇一個檔案(它正在作業)并將其發送到 WebMethod 以上傳它。
我已設法選擇檔案并將其發送到我的 WebMethod。但是,我不確定如何將物件轉換為 C# 中的可讀格式。這是一個 CSV 檔案。
Javascript:
<script>
//formstone file drop picker library
$(".upload").upload({
beforeSend: onBeforeSend
});
function onBeforeSend(formData, file) {
if (file.name.indexOf(".csv") < 0) {
return false;
}
//file is successfully received here
var fd = new FormData();
fd.append(file.name, file);
//this gets called as well
PageMethods.set_path("manage-products.aspx");
PageMethods.UploadCSV(file, onSuccess, onFailure);
function onSuccess(response) {
}
function onFailure() {
console.log("FAIL");
}
return formData;
}
</script>
C#網路方法:
[WebMethod]
public static void UploadCSV(object formData)
{
//I'm trying to get the file data and convert to a readable file here
}
uj5u.com熱心網友回復:
我覺得這個問題已經在這里回答了。在 ac#控制器中接受檔案有幾種方法:
其中之一是這樣的:
[APIController]
[HttpPost]
public ActionResult Post(IFormFile myFile)
{
if(myFile==null)
{//ErrorHandling
return BadRequest();
}
//do what ever you please with your file here
return Ok();
}
上面的示例是我理解和實作的最簡單的方法。也許它對你有用。只需在此控制器上打開 Swagger,在瀏覽器中按 F12 并使用端點。您將在瀏覽器開發工具的網路部分看到如何實作對控制器的請求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/524384.html
標籤:C#网欧文
上一篇:如何將通過IISExpress運行的Web服務連接到SQLServer
下一篇:注入前如何修改IOptions?
