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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從我的服務器下載 zip 文件時遇到問題

從我的服務器下載 zip 文件時遇到問題

C#
慕蓋茨4494581 2023-09-24 17:21:28
我已經(jīng)設置了一個服務器端點,它將壓縮文件文件夾并返回 zip 文件。在客戶端,我有調(diào)用端點并將下載的 zip 文件保存到磁盤的代碼。所有代碼都會運行,但生成的文件比服務器上的 zip 文件大,如果我嘗試打開生成的 zip 文件,則會收到“Windows 無法打開該文件,文件無效”。我究竟做錯了什么?服務器代碼:    [Route("projects/files/download")]    [HttpPost]    public ActionResult Post([FromForm] DownloadFileRequest request)    {               string filesPath = ...;        string zipName = ...;        if (!Directory.Exists(filesPath)) {`            return BadRequest("File path not found on server");        }        if (System.IO.File.Exists(zipName)) System.IO.File.Delete(zipName);        System.IO.Compression.ZipFile.CreateFromDirectory(filesPath, zipName);        byte[] fileBytes = System.IO.File.ReadAllBytes(zipName);        FileContentResult zipFile = File(fileBytes, "application/zip", fileName);        return Ok(zipFile);    }客戶端代碼:    Uri uri = new Uri("https://.../projects/files/download");    response = client.PostAsync(uri.ToString(), formContent).Result;    if (response.IsSuccessStatusCode)`    {        using (HttpContent content = response.Content)        {            Stream stream = content.ReadAsStreamAsync().Result;            string path = ...;            stream.Seek(0, SeekOrigin.Begin);            using (Stream streamToWriteTo = File.Open(path, FileMode.Create))            {                stream.CopyTo(streamToWriteTo);            }        }    }
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經(jīng)驗 獲得超6個贊

不返回 Ok(zipFile),而是返回文件:

返回文件(fileBytes,“應用程序/zip”,文件名);


查看完整回答
反對 回復 2023-09-24
  • 1 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號