第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 Web API 核心下載文件 - Angular 7

從 Web API 核心下載文件 - Angular 7

C#
哈士奇WWW 2022-10-23 14:08:52
我正在嘗試從服務(wù)器下載文件,但該文件未顯示其原始內(nèi)容,而是顯示 [object Object]。WEB API核心[Authorize(AuthenticationSchemes = "Bearer")][HttpGet]public HttpResponseMessage DownloadContractFile(string fileName){    string contentRootPath = _hostingEnvironment.ContentRootPath;    var folderName = Path.Combine(contentRootPath, FileHandler.ContractFilePath, Convert.ToInt32(User.Identity.Name).ToString());    var path = Path.Combine(folderName, fileName);    var memory = new MemoryStream();    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);    using (var stream = new FileStream(path, FileMode.Open))    {        result.Content = new StreamContent(stream);        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");        result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(path);        result.Content.Headers.ContentType = new MediaTypeHeaderValue(FileHandler.GetContentType(path)); // Text file        result.Content.Headers.ContentLength = stream.Length;        return result;    }}Angular 代碼:服務(wù)方法  downloadContractFile(fileName: string) {    const obj: any = { fileName: fileName };    const httpParams: HttpParamsOptions = { fromObject: obj } as HttpParamsOptions;    const httpOptions = {      params: new HttpParams(httpParams),      headers: new HttpHeaders({        'Content-Type': 'application/octet-stream',        'Authorization': 'Bearer ' + this.jwt.getToken      })    };       return this.http.get<Array<any>>(`${this.settings.getApiSettings('uri')}/api/contract/DownloadContractFile`, httpOptions).map( /// <<<=== use `map` here      (response) => {        if (response["status"] == 401) {          this.jwt.redirectToLogin();        }        else if (response["status"] < 200 || response["status"] >= 300) {          throw new Error('This request has failed ' + response["status"]);        }        let data = response;        return data;      }    );  }
查看完整描述

1 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

這是我在項(xiàng)目中用來下載文件的代碼。


控制器代碼:


    [HttpGet("DownloadFile")]

    public async Task<IActionResult> DownloadFile(string fileName = "")

    {

        var response = await DownloadFileFromDatabase(fileName);

        if (response.IsSuccessStatusCode)

        {

            System.Net.Http.HttpContent content = response.Content;

            var contentStream = await content.ReadAsStreamAsync();

            var audioArray = ReadFully(contentStream);

            return Ok(new { response = audioArray, contentType = "audio/wav", fileName });

        }

        else

        {

            throw new FileNotFoundException();

        }

    }

客戶端代碼:


  HandleBase64  (data , contentType,fileName ){ 

    let byteCharacters = atob(data);    

    let byteNumbers = new Array(byteCharacters.length);    

    for (var i = 0; i < byteCharacters.length; i++) 

        byteNumbers[i] = byteCharacters.charCodeAt(i);


    let byteArray = new Uint8Array(byteNumbers);    

    let blob = new Blob([byteArray], {type: contentType});

    if(contentType === "audio/wav"){

        var blobURL=URL.createObjectURL(blob);

        window.open(blobURL);       

    }

    else{

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

        var anchor = document.createElement("a");

        anchor.download = fileName;

        anchor.href = blobURL;

        anchor.click();

    }

}

您也可以在控制器端使用文件流簡(jiǎn)單地返回文件。這將自動(dòng)下載不需要在客戶端處理的文件


    return File(stream, "application/octet-stream"); 


查看完整回答
反對(duì) 回復(fù) 2022-10-23
  • 1 回答
  • 0 關(guān)注
  • 98 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)