vb.net ajax上传文件,VB.net FileUpload using Ajaxcall

Can be accomplished by following the instructions at this link: http://www.binaryintellect.net/articles/f2a2f1ee-e18a-416b-893e-883c800f83f4.aspx

"Instead of making a full page postback you can use jQuery to make an Ajax call to the server and POST the selected files to a generic handler (.ashx). The generic handler can then save the files to a specified folder. The remainder of this post shows how this can be accomplished."

$(document).ready(function () {

$("#Button1").click(function (evt) {

var fileUpload = $("#FileUpload1").get(0);

var files = fileUpload.files;

var data = new FormData();

for (var i = 0; i < files.length; i++) {

data.append(files[i].name, files[i]);

}

var options = {};

options.url = "FileUploadHandler.ashx";

options.type = "POST";

options.data = data;

options.contentType = false;

options.processData = false;

options.success = function (result) { alert(result); };

options.error = function (err) { alert(err.statusText); };

$.ajax(options);

evt.preventDefault();

});

});

Then in your Handler file, you can do something like this to save the POSTed files to your server.

namespace jQueryFileUploadDemo

{

public class FileUploadHandler : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

if (context.Request.Files.Count > 0)

{

HttpFileCollection files = context.Request.Files;

for (int i = 0; i < files.Count;i++ )

{

HttpPostedFile file = files[i];

string fname = context.Server.MapPath("~/uploads/" + file.FileName);

file.SaveAs(fname);

}

}

context.Response.ContentType = "text/plain";

context.Response.Write("File(s) Uploaded Successfully!");

}

public bool IsReusable

{

get

{

return false;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值