跨域檔案上傳解決方案

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

檔案上傳在項目中是必不可少的一個環節。一般而言,只要有一個上傳頁面就可以了  圖片直接上傳到網站下面。

但是當項目較多,或者項目太大 有好多需要上傳檔案的需求時,一個通用的檔案上傳組件就很有必要了。

而且我需要的是檔案上傳之後是在單獨的伺服器上(外在體現就是網站與圖片不是同一個網域名稱)

這樣檔案上傳頁面與當前網站就需要跨域進行上傳操作了。

上傳過程沒有問題,但是上傳結束之後的回調操作因為不能跨域存取值而無法進行。

 

通過對網上一些文章的整理,方案如下

 

調用檔案上傳組件的頁面(url=http://localhost:2565/.....)

<body>    <form id="fileform" name = "fileform" enctype="multipart/form-data" target="hideFrame" method="post"            action="http://localhost:13124/Upload/FileHandler.ashx?cb=http://localhost:2565/fileUploadCB.htm">        <input type="file" id="fileSelector" name="fileSelector" />    </form>    <iframe id="hideFrame" name="hideFrame" style="display:none;"></iframe>    <input type="button" id="btnUpload" value="上傳" />    <script>        document.querySelector("#btnUpload").onclick = function () {            this.style.backgroundColor = "blue";//上傳過程中按鈕是藍色的            document.querySelector("#fileform").submit();        }        function uploadCallBack(rst) {            document.querySelector("#btnUpload").style.backgroundColor = "";//上傳結束按鈕顏色回複            console.log(rst);        }</script>

 form的target要指定一個隱藏的iframe,這樣當前頁面才不會因為上傳圖片而重新整理。

實際上就相當於在隱藏的iframe中開啟action指定的連結並把form中的資料post過去

至於action的連結中有個參數cb,這個cb的連結在整個操作中至關重要:它是上傳結束後回調操作的第一步。

 

上傳檔案的後台代碼(http://localhost:13124/Upload/FileHandler.ashx)

private const int MAX_UPLOAD_SIZE = 2;/// <summary>/// 上傳檔案/// fileSelector是前台代碼中檔案控制項的name
/// </summary>/// <returns></returns>public string UploadFile(HttpRequest req ,HttpResponse res){ if (req.Files["fileSelector"].ContentLength > MAX_UPLOAD_SIZE * 1024 * 1024) { return String.Format("請上傳{0}M以內的檔案。", MAX_UPLOAD_SIZE); } string uploadFileName = req.Files["fileSelector"].FileName; string path = HttpContext.Current.Server.MapPath(uploadFileName); req.Files["fileSelector"].SaveAs(path); return "";} /// <summary>/// 上傳結束後要重新導向到指定連結/// </summary>/// <param name="context"></param>public void ProcessRequest(HttpContext context){ string result = UploadFile(context.Request, context.Response); if (String.IsNullOrEmpty(result)) { result = "上傳成功"; } string cbSrc = context.Request["CB"]; context.Response.Redirect(cbSrc);}

 

 

注意:cb指定的上傳結束後的連結與調用上傳組件的頁面是同源的。

cb指定的頁面(http://localhost:2565/fileUploadCB.htm)

<html xmlns="http://www.w3.org/1999/xhtml"><body>    <script>        alert("成功Oh Yeah!");        parent.uploadCallBack("上傳成功!");    </script></body></html>

 

 該頁面只是起到一個中轉的作用,用於上傳結束之後將結果提供給首頁面,並執行回呼函數。

 

上面給出的只是基本方案。具體的話  還有很多事情要做,上傳時傳的參數還是要研究一下的  因為這決定了檔案的分類,使用者歸屬等。

大部分工作也沒必要自己寫,有很多成熟的上傳外掛程式

如 FineUploader

跨域檔案上傳解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.