1 回答

TA貢獻1802條經驗 獲得超4個贊
為什么是serialize
表格?此方法創建一個可以發送到服務器的字符串,但這不是您想要做的......
FormData
type 會自動管理enctype
您的表單,因此您可以省略它 - 盡管您應該考慮使用它,因為它可以幫助團隊中的其他成員理解意圖。如果您想使用純 jQuery,您只需將formData
變量附加到data
調用的字段即可$.ajax
??纯催@里,
/*
* i know id-based selection should only have 1 element,?
* otherwise HTML is invalid for containing multiple elements?
* with the same id, but this is the exact code i used back then, so using it again.
**/
var formData = new FormData($('#form')[0]);??
$.ajax({
? ? type: 'POST',
? ? processData: false,
? ? contentType: false,
? ? data: formData,
? ? success: function (data) {
? ? ? ?// The file was uploaded successfully...
? ? ? ?$('.result').text('File was uploaded.');
? ? },
? ? error: function (data) {
? ? ? ?// there was an error.
? ? ? ?$('.result').text('Whoops! There was an error in the request.');
? ? }
});
這當然要求您的 HTML DOM 包含這些元素——我使用了幾年前為我的文章編寫的代碼。其次,對于該功能的其他部分,我曾經Request.Files捕獲可能已隨請求上傳的文件。
files = Request.Files.Count;
if(files > 0) {
? ?// Files are sent!
? ?for (int i = 0; i < files; i++) {
? ? ? var file = Request.Files[i];
? ? ? // Got the image...
? ? ? string fileName = Path.GetFileName(file.FileName);
? ? ? // Save the file...
? ? ? file.SaveAs(Server.MapPath("~/" + fileName));
? ?}
}
這樣,我使用 jQuery 和FormData.
您可以查看我在此處發布的完整文章,上傳文件 — HTML5 和 jQuery 方式!
哦,不要忘記評論中提出的建議,
using (var com = new SqlCommand("dbo.sp_some_stored_procedure_for_saving_data", con))?
{
? ? con.Open(); // missed call?
? ? data = Convert.ToString(com.ExecuteScalar());?
? ? // although, using should close here!
? ? var file_path = Path.Combine(path, data + Extension);
? ? fileUpload.SaveAs(file_path);
}
所以,這就是你可以做到這一點的方法。
- 1 回答
- 0 關注
- 212 瀏覽
添加回答
舉報