My collegue Martijn, have pointed me and my collegues on an issue when elivering an file to download to an user.
You have to clear the headers as Sitecore is destroying the headers oof the request. Use Request.ClearHeaders(); to make begin building your headers from scratch.
Here’s a full download script:
string extentie;
string mimetype = string.Empty;
FileStream sourceFile = null;
FileInfo myInfo = null;
try
{
myInfo = new FileInfo(Server.MapPath("/eg/path/to/file/"));"
sourceFile = myInfo.Open(FileMode.Open);
}
catch(Exception Ex)
{
Response.Write(Ex.Message);
Response.End();
}
extentie = myInfo.Extension;
switch(extentie)
{
case "pdf":
case ".pdf":
mimetype = "application/pdf";
break;
case "jpg":
case ".jpg":
mimetype = "image/JPEG";
break;
case "jpeg":
case ".jpeg":
mimetype = "image/JPEG";
break;
case "gif":
case ".gif":
mimetype = "image/GIF";
break;
case "txt":
case ".txt":
mimetype = "text/plain";
break;
case "doc":
case ".doc":
mimetype = "Application/msword";
break;
}
//Write header
Response.ClearHeaders();
Response.ContentType = mimetype;
Response.AddHeader("content-disposition","attachment; filename="+myInfo.Name);
//Write file
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.BinaryWrite(getContent);
Response.End();
Zeg Alex..wat dacht je van een try, catch, finally om de sourceFile te “closen”!? Using is overigens nog beter!
Take a look at this: http://www.jaggersoft.com/pubs/ExceptionHandlingInCSharp.htm