非同步上傳檔案、圖片

來源:互聯網
上載者:User

標籤:rom   substr   define   return   fine   尺寸   multipart   contents   move   

// 目標頁面上有東西 <input type="file" name="fileField" id="fileField" />function ajaxUpload(opt) {    /*    參數說明:    opt.ID : 頁面裡file控制項的ID;    opt.Format : 檔案格式,以數組的形式傳遞,如[‘jpg‘,‘png‘,‘gif‘,‘bmp‘];    opt.CallBack : 上傳成功後回調;    opt.Url:上傳地址;    opt.ImageSizeArry:圖片壓縮尺寸,如[‘200x200‘,‘100x100‘];    opt.ProxyUrl : 上傳回調Proxy 位址    */    var iName = "frameName" + Math.random().toString(); //太長了,變短點    var iframe, form, file, fileParent;    var postUrl = opt.Url;    if (postUrl.indexOf("?") <= -1)    {        postUrl += "?rd=" + Math.random();    }    if (opt.ImageSizeArry != null && opt.ImageSizeArry != undefined && opt.ImageSizeArry != "") {        postUrl += "&ImageSizeArry=" + opt.ImageSizeArry;    }    if (opt.ProxyUrl != null && opt.ProxyUrl != undefined && opt.ProxyUrl != "") {        postUrl += "&proxyUrl=" + encodeURIComponent(opt.ProxyUrl);    }    //建立iframe和form表單    iframe = $(‘<iframe name="‘ + iName + ‘" style="display:none;"  />‘);    form = $(‘<form method="post" target="‘ + iName + ‘" style="display:none;" action="‘ + postUrl + ‘"  name="form_‘ + iName + ‘" enctype="multipart/form-data" ></from>‘);    file = $(‘#‘ + opt.ID); //通過ID擷取flie控制項    fileParent = file.parent(); //存父級    if (file.val()=="") {        return;    }    file.appendTo(form);    //插入body    $(document.body).append(iframe).append(form);    //取得所選檔案的副檔名    var fileFormat = /\.[a-zA-Z]+$/.exec(file.val())[0].substring(1);    if (opt.Format.join(‘-‘).indexOf(fileFormat) != -1) {        form.submit(); //格式通過驗證後提交表單;    } else {        file.appendTo(fileParent); //將file控制項放回到頁面        iframe.remove();        form.remove();        $.DR_Alter("檔案格式錯誤,請重新選擇!");    };    //檔案提交完後    iframe.load(function () {        var data = $(iframe).contents().find(‘body‘).html();        file.appendTo(fileParent);        iframe.remove();        form.remove();        opt.CallBack(data, opt.ID);    })}

  

非同步上傳檔案、圖片

聯繫我們

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