1

I have a API method where I am create zip file in a stream

MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

FileInfo fi = new FileInfo(version.File.ContentFilePath);

string entryName = ZipEntry.CleanName(fi.Name);
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);

byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(fi.FullName))
{
   StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
zipStream.IsStreamOwner = false;
zipStream.Close();

outputMemStream.Position = 0;

long fileLength;
long startOffset;
long endOffset;
bool isPartial = false;
string errMessage = string.Empty;
Stream fs = FileUpDownHelper.GetPartialFileStream(outputMemStream, out fileLength, out startOffset, out endOffset, out isPartial, out errMessage);

return fs;

Then I need send this file, but I have a problem, that I cannot change the file name. And file name always = method name. How can I fix this without creating a file?

Unnamed
  • 15
  • 8
  • Where is your controller method? How are you returning the file? There is an overload of `File` that you set the filename. – Magnetron Mar 01 '21 at 17:43
  • @Magnetron this is all method, Zip file don't have overload and i can't set the filename. I return Stream(zip in it the file). My method have name Download, and I download - "Download" file. – Unnamed Mar 01 '21 at 17:50
  • Filenames are handled in your response. there are many duplicates of this question. here's one that should help https://stackoverflow.com/questions/74019/specifying-filename-for-dynamic-pdf-in-asp-net – Brett Caswell Mar 01 '21 at 17:51
  • You can return like `return File(fs, "application/zip", "MyFileName.zip");` – Magnetron Mar 01 '21 at 17:55
  • @BrettCaswell I cannot return anything but a stream, this solution does not work for me – Unnamed Mar 01 '21 at 17:55
  • @Magnetron I can return only Stream – Unnamed Mar 01 '21 at 17:56
  • Why is that? Also, Brett dupe isn't changing the return type, it's just adding http headers to the http response. – Magnetron Mar 01 '21 at 17:58
  • you need to be clearer how what is inhibiting you @Unnamed octet-stream response body with the Content-Disposition header not itself uncommon nor inhibiting. Just ensure you're setting the `Content-Disposition` – Brett Caswell Mar 01 '21 at 18:02
  • @Magnetron if I send anything other than Stream, I have an error. Error: read ECONNRESET. I think that I cannot change name without create it – Unnamed Mar 01 '21 at 18:06

0 Answers0