jquery.html5uploader.js 上傳控制項

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   ar   檔案   資料   

外掛程式地址:http://blog.csdn.net/never_say_goodbye/article/details/8598521

先上個:

相比來說,效果還是很不錯的

使用MVC3做伺服器端的處理時遇到的一大問題是HttpPostedFileBase為null,request.file也為null,這是上面的部落格中沒有提到的(PHP),一番潛心研究中,發現了Request.InputStream中有資料,於是乎:

[HttpPost]        public JsonResult FileUpload()        {            Stream RequestStream =HttpContext.Request.InputStream;//把檔案的資料讀取到st流中            if (RequestStream == null)            {                return Json(new { msg=0,msgbox="上傳的檔案為空白"});//"上傳的檔案為空白";            }            string file_name =  HttpContext.Request.Headers["X_FILENAME"].ToString();            if(string.IsNullOrWhiteSpace(file_name))            {                return Json(new { msg = 0, msgbox = "檔案名稱為空白" });//"檔案名稱為空白";            }            int last_Index = file_name.LastIndexOf(".");            string fileExt = file_name.Substring(last_Index+1,file_name.Length -last_Index -1); //得到檔案的拓展名,不包含“.”            string saveFloder = Server.MapPath("/uploads/"); //儲存的檔案路徑            if (!Directory.Exists(saveFloder)) //如果該檔案夾不存在, 則建立            {                Directory.CreateDirectory(saveFloder);            }            string save_name = Guid.NewGuid().ToString()+"."+fileExt; //儲存的檔案名稱            ////建立一個檔案流來把上傳的檔案寫入到伺服器上            FileStream fs = new FileStream(saveFloder + save_name, FileMode.Create, FileAccess.Write, FileShare.Read);            byte[] buffer = new byte[1024];//緩衝區            int readLen;//每次真正讀取到的位元組數            while ((readLen = RequestStream.Read(buffer, 0, 1024)) > 0)            {                //讀到資料了(此時獨到的資料在buffer緩衝區中)                //開始把檔案寫入到伺服器的磁碟中                fs.Write(buffer, 0, readLen);            }            fs.Flush();//請空緩衝區,使得所有緩衝區的資料都寫入到檔案中            RequestStream.Flush();            fs.Close();//關閉流,釋放資源            RequestStream.Close();            return Json(new { msg = 1, msgbox = "/uploads/" + save_name });        }

目前沒有測試過大檔案上傳的情況及其效能

 

jquery.html5uploader.js 上傳控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.