amitshrivas 0 Light Poster

Hello to all..plz help me in this plzzzzzzzzzzzzz
i want to view/download a files from production server and after downloading i want to delete the same file from 2 places i.e from (database and one Uploaded folder in server).
i have 2 quries:

1: View Only ?
i have this code to view a file but it won't work for .xls files

Response.Clear();
   Response.AppendHeader("content-disposition", "inline;   
   filename=" + filename);
   Response.ContentType = conttype;
   Response.TransmitFile(Server.MapPath("~") + @"\Schedule\" + 
   filename);
   Response.End();
   Response.Flush();

this code works perfectly for .txt and .doc file but wont work for .xls means This will show open/Save Filedialog box which i dont want to see if i click on View Link

2: i want to download a fle from production server.
and the code i have written is

Response.Clear();
     Response.AppendHeader("content-disposition", "attachment; filename="+ filename);
        Response.ContentType = cont;
        string filepath = Server.MapPath("~") + @"\Schedule\" + filename;
        Response.WriteFile(filepath);
       
//        DeletRecordFromDatabase();
//        File.Delete(filepath);
//        LoadSchedule();
        Response.End();
        
        Response.Flush();

please read the code carefully..
specially in between this

Response.WriteFile(filepath);
       
//        DeletRecordFromDatabase();
//        File.Delete(filepath);
//        LoadSchedule();
        Response.End();
Response.Flush();

the commented part are the functions through which i will delete the file form Database,from Folder and Reload all the fresh data from database..to gridview
but when i click on Download link then it will not show the Open/Save Dialog box rather it will delete the file form database and from folder too.
and when i write the code something like this

Response.WriteFile(filepath);
        Response.End();
//        DeletRecordFromDatabase();
//        File.Delete(filepath);
//        LoadSchedule();
Response.Flush();

then it will show the Open/Save Dialog box but will not delete the data from database and from folder..

please help its urgent..

Dani AI

Generated

Two separate problems are being mixed: why Excel (.xls) usually doesn’t open "inline" in the browser, and why deleting the file in the same request sometimes runs and sometimes doesn’t. Both are normal behavior and have simple, reliable fixes.

Browsers decide whether a file is shown inline or offered as a download. Sending Content-Disposition:inline only requests inline display; many browsers do not render Office binary files and will show an Open/Save dialog. Confirm the correct MIME type (for old Excel: application/vnd.ms-excel) or use MimeMapping.GetMimeMapping(filename) to set it automatically. If you must guarantee in-browser preview, convert the workbook server-side to PDF or HTML and stream that instead (PDFs are widely viewable in modern browsers). See the Content-Disposition notes here: MDN — Content-Disposition and MIME lookup here: MimeMapping.GetMimeMapping.

Do not rely on Response.End() or deleting the file synchronously after starting the response. Response.End() aborts the thread (so code after it won’t run) and deleting the file while it’s being transmitted causes race conditions. See HttpResponse.End() and TransmitFile details here: HttpResponse.End and HttpResponse.TransmitFile.

Recommended pattern: stream the file to the response and schedule the delete/update in a background task (or use a periodic cleanup job). Example (simplified):

Response.BufferOutput = false;
Response.ContentType = MimeMapping.GetMimeMapping(fileName);
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    var buffer = new byte[64*1024];
    int read;
    while ((read = fs.Read(buffer,0,buffer.Length))>0)
    {
        Response.OutputStream.Write(buffer,0,read);
        Response.Flush();
        if (!Response.IsClientConnected) break;
    }
}

HttpContext.Current.ApplicationInstance.CompleteRequest();

// run deletion in background so streaming is not interrupted
ThreadPool.QueueUserWorkItem(_ => {
    try { File.Delete(filePath); /* update DB here */ }
    catch { /* log */ }
});

Also ensure the web app identity has delete permissions and validate filenames to avoid path-traversal. For a robust production solution, use a background queue (e.g., HostingEnvironment.QueueBackgroundWorkItem if available) or a scheduled cleanup job rather than attempting a synchronous delete in the same HTTP request. This addresses both the .xls viewing expectation and the inconsistent delete behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.