Có bạn có thể đạt được điều này qua đường bưu điện ajax phương pháp. ở phía máy chủ, bạn có thể sử dụng httphandler. Vì vậy, chúng tôi không sử dụng bất kỳ điều khiển máy chủ nào theo yêu cầu của bạn.
với ajax, bạn cũng có thể hiển thị tiến trình tải lên.
bạn sẽ phải đọc tệp dưới dạng đầu vào.
using (FileStream fs = File.Create("D:\\_Workarea\\" + fileName))
{
Byte[] buffer = new Byte[32 * 1024];
int read = context.Request.GetBufferlessInputStream().Read(buffer, 0, buffer.Length);
while (read > 0)
{
fs.Write(buffer, 0, read);
read = context.Request.GetBufferlessInputStream().Read(buffer, 0, buffer.Length);
}
}
Mẫu Mã
function sendFile(file) {
debugger;
$.ajax({
url: 'handler/FileUploader.ashx?FileName=' + file.name, //server script to process data
type: 'POST',
xhr: function() {
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
}
return myXhr;
},
success: function (result) {
//On success if you want to perform some tasks.
},
data: file,
cache: false,
contentType: false,
processData: false
});
function progressHandlingFunction(e) {
if (e.lengthComputable) {
var s = parseInt((e.loaded/e.total) * 100);
$("#progress" + currFile).text(s + "%");
$("#progbarWidth" + currFile).width(s + "%");
if (s == 100) {
triggerNextFileUpload();
}
}
}
}
Nguồn
2014-03-27 08:48:00
Ahhh .... ngày newb của tôi ... :) –
lmao @Ronnie Overby –