2

I've got a legacy app that allows upload and download of files. The files are stored on disc the files names in a SQL database. To allow duplicate files names from the users, the database store the user original file name and a unique name. The files are displayed in a .NET repeater with the command argument supplying both files names (original and unique). When the file is downloaded the code below serves up the file to the user with the original name.

All works fine on PC with Chrome, IE, FFox. For Mac user, they get FileName.ext.jpeg when they should be getting just FileName.ext

I'm not very well versed in Mac stuff. Is there something I can do to ContentType or Content-Dispositon to fix this for the few Mac users and not impact my large PC user base?

Here is the code to rename the file stored on the server with the original name:

LinkButton btn = (LinkButton)(sender);
string sURL = "";
string sFileName = btn.CommandArgument;

string sOriginalFileName = sFileName.Substring(0, sFileName.LastIndexOf("~"));
string sServerFileName = sFileName.Substring(sFileName.LastIndexOf("~") + 1); ;

sURL = sResourceRoot + "\\" + sServerFileName;

Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + sOriginalFileName);
Response.TransmitFile(Server.MapPath(@"~/" + sURL));
Response.End();
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
RichP
  • 525
  • 1
  • 10
  • 25
  • 1
    When you say "Mac users", do you mean the when the download happens via Safari? – Dean Goodman Aug 15 '18 at 18:43
  • I'm waiting for an answer back from the small user base. They are not very well versed in computers to say it nicely. I believe it is Safari. I've asked them to try a different browser on their computers. For now I know it is a MacBook and that is about it – RichP Aug 15 '18 at 18:46

1 Answers1

1

Generally, you should quote your filename in the Content-Disposition header -- see here. Note also that unexpected filename characters could play a role in your issue. It's probably worth using the built-in .NET ContentDispositionHeaderValue class to help create your header.

Try this - it should also be safe for all the other browsers you list.

Response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = sOriginalFileName };

Dean Goodman
  • 973
  • 9
  • 22