asmx 接受 ajax post,使用jQuery Ajax将文件发送到C#中的Web服务(asmx)

这篇博客介绍了如何使用FormData和Ajax实现文件上传功能。通过jQuery选择上传控件获取文件,将文件添加到FormData中,然后设置contentType为false和processData为false进行Ajax请求。在服务器端,可以通过HttpContext.Current.Request.Files获取上传的文件并保存到指定目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小编典典

您需要序列化数据…。

var data = new FormData();

var files = $("#YOURUPLOADCTRLID").get(0).files;

// Add the uploaded image content to the form data collection

if (files.length > 0) {

data.append("UploadedFile", files[0]);

}

// Make Ajax request with the contentType = false, and procesDate = false

var ajaxRequest = $.ajax({

type: "POST",

url: "/api/fileupload/uploadfile",

contentType: false,

processData: false,

data: data

});

在控制器内部,您可以拥有类似

if (HttpContext.Current.Request.Files.AllKeys.Any())

{

// Get the uploaded image from the Files collection

var httpPostedFile = HttpContext.Current.Request.Files["UploadedFile"];

if (httpPostedFile != null)

{

// Validate the uploaded image(optional)

// Get the complete file path

var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);

// Save the uploaded file to "UploadedFiles" folder

httpPostedFile.SaveAs(fileSavePath);

}

}

希望能帮助到你…

2020-07-26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值