我已經(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); } } }
從我的服務器下載 zip 文件時遇到問題
慕蓋茨4494581
2023-09-24 17:21:28