1. 程式人生 > 其它 >利用Blob從api返回的檔案流下載檔案

利用Blob從api返回的檔案流下載檔案



function downLoad(data){
    var url = window.serverIP+"/Document/BatchDownLoadPLMDocument";
    $.ajax({
        url,
        data:JSON.stringify(data),
        type:"POST",
        dataType:'binary',
        contentType: "application/json",
        xhrFields: {
            responseType: "blob"
        },
        headers:{"Authorization":"Bearer "+window.token,'Content-Type':'application/json','X-Requested-With':'XMLHttpRequest'},
        success:function(fileRes){
            let blob = new Blob([fileRes]);
            let src = window.URL.createObjectURL(fileRes);
            $('#photo').attr('src',src);
            document.getElementById('photoName').innerHTML = $d("workOrderSchedulePathDTO.materialName")+"("+$d("workOrderSchedulePathDTO.materialCode")+")";
            $('#photo').on("tap",function(){
                Mobile_NS.previewImg(src);
            });
            $('#photo').on("click",function(){
                Mobile_NS.previewImg(src);
            });
        },
        fail:function(fileRes){
            Mobile_NS.alert(fileRes);
            
        },
        complete: function(xhr){
            Mobile_NS.hideLoader();
        }
    });
}
function downLoad(data){
    var url = window.serverIP+"/Document/BatchDownLoadPLMDocument";
    $.ajax({
        url,
        data:JSON.stringify(data),
        type:"POST",
        dataType:'binary',
        contentType: "application/json",
        xhrFields: {
            responseType: "blob"
        },
        headers:{"Authorization":"Bearer "+window.token,'Content-Type':'application/json','X-Requested-With':'XMLHttpRequest'},
        success:function(fileRes){
            let blob = new Blob([fileRes]);
            let src = window.URL.createObjectURL(fileRes);
            $('#photo').attr('src',src);
            document.getElementById('photoName').innerHTML = $d("workOrderSchedulePathDTO.materialName")+"("+$d("workOrderSchedulePathDTO.materialCode")+")";
            $('#photo').on("tap",function(){
                Mobile_NS.previewImg(src);
            });
            $('#photo').on("click",function(){
                Mobile_NS.previewImg(src);
            });
        },
        fail:function(fileRes){
            Mobile_NS.alert(fileRes);
            
        },
        complete: function(xhr){
            Mobile_NS.hideLoader();
        }
    });
}