最近在一個ASP.NET 項目中使用了plupload 來上傳文件,結果幾天後客戶發郵件說上傳的文件不對,說是文件沒法打開php
var plupload=$("#uploader").plupload({ //解決分塊上傳時文件名爲"blob"的bug init:{ BeforeUpload:function(up, file){ // Called right before the upload for a given file starts, can be used to cancel it if required up.settings.multipart_params ={ filename: file.name }; }, } });
/// <summary> /// 上傳文件 /// </summary> /// <param name="context"></param> public void UploadFile(HttpContext context) { if (context.Request.Files.Count <= 0) return; context.Response.CacheControl = "no-cache"; string s_rpath = "E:\website\temp"; //是不是分塊上傳 bool isChunk = false; //上傳文件路徑 string filepath = string.Empty; FileStream fs = null; //字節 Byte[] buffer = null; //須要上傳的文件 HttpPostedFile httpUploadFile = null; //分塊上傳的文件名,結合js用於解決使用分塊上傳時當前文件名爲"blob"的bug string filename = context.Request["filename"]; //存在塊參數 if (!string.IsNullOrEmpty(context.Request.Params["chunk"])) { int chunk_number = 0; int.TryParse(context.Request.Params["chunk"], out chunk_number); isChunk = chunk_number > 0;//分塊上傳,第一塊爲0 } try { for (int i = 0; i < context.Request.Files.Count; i++) { #region 上傳文件 //文件列表 httpUploadFile = context.Request.Files[i]; //塊文件名不存在則使用當前文件的文件名 if (string.IsNullOrEmpty(filename) || filename.Length <= 0) filename = httpUploadFile.FileName; filepath = System.IO.Path.Combine(s_rpath, filename); //對文件流進行存儲 //若是是塊上傳則寫入文件不然建立文件 fs = new FileStream(filepath, isChunk ? FileMode.OpenOrCreate : FileMode.Append); buffer = new Byte[httpUploadFile.InputStream.Length]; httpUploadFile.InputStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); fs.Close(); #endregion } } catch (Exception ex) { context.Response.Write("error:" + ex.ToString()); } }
關於plupload的使用心得
html
ps:html5
爲知筆記噁心的強制收費致使很是的不爽git
因此準備把未上傳到博客的爲知筆記文章陸續上傳github