close

C#-Side:

FileStream fs = File.OpenRead("path/to/file");

int length = (int)fs.Length;

byte[] buffer ;

BinaryReader binaryRD = new BinaryReader(fs);

buffer = binaryRD.ReadBytes(length);

response.AddHeader("Content-Disposition","attachment=" + fileName);

response.ContentType = "application/pdf";

response.BinaryWrite(buffer);

response.Flush();

response.Close();

response.End();

 

Javascript-Side:

var url = '.......';

var xhr = new XMLHttpRequest();

xhr.open("GET",url,true);

xhr.responseType = 'arraybuffer';

xhr.addEventListener('load',function(){

if(xhr.status === 200){

var blob = new Blob([xhr.response],{type:"application/pdf",endings:'native'});

var link = document.createElement('a');

var url = window.URL.createObjectURL(blob);

link.href = url;

link.download = fileName;

link.click();

}

});

xhr.send();

arrow
arrow
    文章標籤
    C# javascript .net
    全站熱搜

    周道格 發表在 痞客邦 留言(0) 人氣()