亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

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

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

C#
慕蓋茨4494581 2023-09-24 17:21:28
我已經設置了一個服務器端點,它將壓縮文件文件夾并返回 zip 文件。在客戶端,我有調用端點并將下載的 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條經驗 獲得超6個贊

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

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


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號