申请序号: | |
选择文件: | type="file" accept=".pdf" style="width: 300px" /> |
function w_file_bnt_qd() {
var file = document.getElementById('w_file_input_file').files[0];
var formData = new FormData();
alert(file.name);
formData.append('pdf', file);
$.ajax({
url: "w_file_upload.aspx",
type: 'POST',
data: formData,
processData: false,
contentType: false,
async: false,
cache: false,
success: function(data) {
alert(data);
}
});
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class htgl_ht_hqsq_w_file_upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string fileName="";
HttpContext context = HttpContext.Current;
context.Response.ContentType = "text/plain";
//获取所有的上传文件
//如果上传的文件超过了4M,则会报异常[超过了最大请求长度。],需要在Web.config配置
HttpFileCollection hfc = context.Request.Files;
//如果是hfc.Count为0,则可以认为是直接请求该ashx处理程序
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
//大小限制
int maxSize = 5 * 1024 * 1024; //最大是2M(转换为字节)
if (hpf.ContentLength > maxSize)
{
context.Response.Write("上传文件大小超出限制");
return;
}
fileName =Server.MapPath("~/htgl/pdf/")+ "1.pdf";
hpf.SaveAs(fileName);
}
}
context.Response.Write("上传成功:"+fileName);
}
}