未选择任何文件
引入 <script src="../javaScript/ajaxfileupload.js"></script>
<button class="btn btn-success" id="upload">上传文件</button> <input type="file" id="btnUpload" name="file" accept=".xls,.xlsx" style="display: none;" /> <input type="hidden" id="filepath" /> <input type="hidden" id="filename" /> <span id="uploadfilename">未选择任何文件</span></li>
参考:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html
$("#upload").bind("click", function () { //选择上传文件 $("#btnUpload").click(); }); $("#btnUpload").live("change", function () { $.ajaxFileUpload({ url: '/UploadFiles.aspx', //用于文件上传的服务器端请求地址 secureuri: false, //一般设置为false fileElementId: 'btnUpload', //文件上传空间的id属性 <input type="file" id="file" name="file" /> dataType: 'json', //返回值类型 一般设置为json success: function (data, status) //服务器成功响应处理函数 { $("#filepath").val(data.filepath); $("#filename").val(data.filename); $("#uploadfilename").html(data.filename); $("#btnUpload").val(""); }, error: function (data, status, e) //服务器响应失败处理函数 { alert(e); } }) });
public partial class UploadFiles : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var files = Request.Files; string msg = string.Empty; string error = string.Empty; string filepath; string urlpath = HttpContext.Current.Request.Url.Host; string uploadurl = Server.MapPath("/") + "Tempalte\upload\";//files[0].FileName.Split('.')[files[0].FileName.Split('.').Length-1] //string fname = DateTime.Now.ToString("yyMMdHHmmssfff") + System.IO.Path.GetFileName(files[0].FileName); string fname = DateTime.Now.ToString("yyMMdHHmmssfff") + System.IO.Path.GetExtension(files[0].FileName); if (!Directory.Exists(uploadurl)) { Directory.CreateDirectory(uploadurl); } if (files.Count > 0) { files[0].SaveAs(uploadurl + fname); msg = " 成功! 文件大小为:" + files[0].ContentLength; filepath = "/Tempalte/upload/" + fname; string res = "{ error:'" + error + "', msg:'" + msg + "',filename:'" + files[0].FileName + "',filepath:'" + filepath + "'}"; Response.Write(res); Response.End(); } } }