0

I have try to code for user to download the .dat file by using Response.Redirect. However, it always show me this error.

The page you are requesting cannot be served because of the extension 
configuration. If the page is a script, add a handler. If the file should be
downloaded, add a MIME map.

My sample code for Response.redirect

Response.Redirect(filepath, false);

I don't no what wrong with it. Plus,I can download .xlsx file by using this code.

Thanks for helping me.

This is the updated code that have no response to download.
EDITED CODE

string fpath = Server.MapPath("data") + "\\" + filename + ".dat";
 if (File.Exists(fpath))
{
File.Delete(fpath);
}
StreamWriter swExtLogFile = new StreamWriter(fpath, true);
try
{
int i;
foreach (DataRow row in tbl.Rows)
{
object[] array = row.ItemArray;
for (i = 0; i < array.Length - 1; i++)
{
swExtLogFile.Write(array[i].ToString());
}
swExtLogFile.WriteLine(array[i].ToString());
}
swExtLogFile.Close();
swExtLogFile.Dispose();
FileInfo fInfo = new FileInfo(fpath);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", fInfo.Length.ToString());
Response.AppendHeader("content-disposition", 
string.Format("attachment;filename={0}", fInfo.Name));
 Response.Flush();
Response.TransmitFile(fInfo.FullName);
HttpContext.Current.ApplicationInstance.CompleteRequest();    
Ling
  • 87
  • 1
  • 13
  • Have u checked on IIS to add the MIME type in? – Jacky Aug 07 '17 at 02:56
  • 1
    Possible duplicate of ["The page you are requesting cannot be served because of the extension configuration." error message](https://stackoverflow.com/questions/4388066/the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configura) – Lance U. Matthews Aug 07 '17 at 02:58

1 Answers1

0

Try this

You don't have to do MIME map.

private void dwnldFile(string filepath)
{
    FileInfo fInfo = new FileInfo(filepath);
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Length", fInfo.Length.ToString);
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", fInfo.Name));
    Response.Flush();
    Response.TransmitFile(fInfo.FullName)
    Response.End();
}
Sankar
  • 6,908
  • 2
  • 30
  • 53
  • Thanks for reply me. But this error have thrown after adding the code System.Threading.ThreadAbortException: Thread was being aborted. This error have shown before also, but I not actually know what have cause it. – Ling Aug 07 '17 at 03:16
  • You have to show your code for better understanding. – Sankar Aug 07 '17 at 03:51
  • The System.Threading.ThreadAbortException: Thread was being aborted. is coming from Response.End() – Ling Aug 07 '17 at 04:28
  • Are you using thread? if so can you do this after the execution of thread? – Sankar Aug 07 '17 at 04:29
  • Replace Response.End() with HttpContext.Current.ApplicationInstance.CompleteRequest(); – ManishKumar Aug 07 '17 at 06:19
  • Ok, now don't have any error shown, but I don't seen my file have been download. – Ling Aug 07 '17 at 07:54
  • I have edited the code. I found out that Response.Transmit code did not pop out any download dialog even the code is go through it. – Ling Aug 08 '17 at 01:37