文件上傳之Html5 + jQuery上傳、asp.net web api接收


HTML:

  <div>
        <label for="fileUpload">
            選擇文件
        </label>
        <br/>
        <input id="fileUpload" type="file" multiple="multiple" />
        <br />
        <input id="btnUploadFile" type="button" value="上傳文件" />
    </div>

 

js:

$("#btnUploadFile").on("click", function () {
                var data = new FormData(); 
                var files = $("#fileUpload").get(0).files;
                if(files.length > 0){
                    for (var i = 0; i < files.length;i++){
                data.append(i.toString(), files[i]); } } $.ajax({ type: "post", url: "http://localhost:7247/api/fdfs/upload",  contentType: false, cache: false, currentType: false, processData: false, data: data, success: function (res) { alert(res); } }); });

WebAPI:

         if(HttpContext.Current.Request.Files.AllKeys.Any())
            {
                var httpPostedFile = HttpContext.Current.Request.Files;
                if(httpPostedFile != null && httpPostedFile.Count > 0)
                {
                    foreach (string f in httpPostedFile)
                    {
                        var file = httpPostedFile[f];
                        //Todo:文件處理操作
                    }
                }
            }    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM