This is a migrated thread and some comments may be shown as answers.

File explorer handler not working for JPEG

1 Answer 10 Views
FileExplorer
This is a migrated thread and some comments may be shown as answers.
Suzan
Top achievements
Rank 1
Suzan asked on 09 Oct 2018, 10:23 AM

I am using this code in my handler.ashx file

public void ProcessRequest(HttpContext context)
{
Context = context;
string filePath = context.Request.QueryString["path"];
filePath = context.Server.MapPath(filePath);
if (filePath == null)
{
return;
}
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
byte[] bytes = new byte[streamReader.BaseStream.Length];
br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
if (bytes == null)
{
return;
}
streamReader.Close();
br.Close();
string extension = System.IO.Path.GetExtension(filePath);
string fileName = System.IO.Path.GetFileName(filePath);
if (extension == ".jpg")
{ // Handle *.jpg and
  WriteFile(bytes, fileName, "image/jpeg", context.Response);
}
else if (extension == ".gif")
{// Handle *.gif
WriteFile(bytes, fileName, "image/gif gif", context.Response);
}
else if (extension == ".pdf")
{// Handle *.pdf
WriteFile(bytes, fileName, "application/pdf pdf", context.Response);
}
}
/// <summary>
/// Sends a byte array to the client
/// </summary>
/// <param name="content">binary file content</param>
/// <param name="fileName">the filename to be sent to the client</param>
/// <param name="contentType">the file content type</param>
private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)
{
response.Buffer = true;
response.Clear();
response.ContentType = contentType;
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.BinaryWrite(content);
response.Flush();
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}

It doesn't work for JPEG files. It just opens a blank page.

What am I doing wrong?

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 11 Oct 2018, 11:55 AM
Hi Suzan,

Can you try replacing this code:
if (extension == ".jpg")
{ // Handle *.jpg and
  WriteFile(bytes, fileName, "image/jpeg", context.Response);
}

with the following one:
if (extension == ".jpg")
{ // Handle *.jpg and
    WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);
}

and see what the result will be?

The second one is taken from the following live demo, where the *.jpg images are downloaded successfully:
https://demos.telerik.com/aspnet-ajax/fileexplorer/examples/applicationscenarios/filteranddownloadfiles/defaultcs.aspx

For convenience, I am attaching the whole handler implementation, so you can examine it and compare it with the one used by you.

Regards,
Vessy
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
FileExplorer
Asked by
Suzan
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or