背景:在用戶控件中使用html 的file控件或者ASP.NET的FileUpLoad控件都沒法獲取到文件,因而想到據說過的uploadifyjavascript
uploadify官網:www.uploadify.comphp
直接官網下載相關文件,發現裏面有幾個php文件,爲了避免用還配置php因而修改成ASP.NET用法css
添加UploadHandler.ashx文件及修改代碼:html
public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"]; string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\"; if (file != null) { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } file.SaveAs(uploadPath + file.FileName); //下面這句代碼缺乏的話,上傳成功後上傳隊列的顯示不會自動消失 context.Response.Write("1"); } else { context.Response.Write("0"); } }
在ascx頁面引入3.1版本的uploadifyjava
<link rel="stylesheet" type="text/css" href="../jslib/uploadify/uploadify.css" /> <script type="text/javascript" src="../jslib/uploadify/jquery-1.7.2.min.js"></script>//uploadify官網說明1.7.2+,既然推薦1.7.2那就是這個版本 <script type="text/javascript" src="../jslib/uploadify/jquery.uploadify.min.js"></script>
在加入jsjquery
$(function() {
$('#ftcFormHolder_FpgForm_btnHtmlFile1').uploadify({
'swf' : '../jslib/uploadify/uploadify.swf',
'uploader' : '../jslib/uploadify/UploadHandler.ashx',
'buttonText': '瀏覽',
'multi': false,
'onUploadComplete' : function(file) {
alert('The file ' + file.name + ' finished processing.');
}
});
});
以上爲簡單使用,其餘參數或者事件可參考官網spa