Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 20, 2012 09:27 AM by shivanand G N
Member
433 Points
100 Posts
Aug 17, 2012 06:04 AM|LINK
Hi All,
i am having trouble when attempting to delete file after download
HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + XlsFilename); HttpContext.Current.Response.OutputStream.Write(data1, 0, data1.Length); HttpContext.Current.Response.Flush();
System.IO.File.Delete(XlsPathAndFilename); //Line of Error HttpContext.Current.Response.End();
opens SaveFiledialog File.Delete gives "Used by another process error". How do i know that user has downloaded the file or cancelled the dialog ??
Star
7625 Points
1454 Posts
Aug 17, 2012 06:23 AM|LINK
before deleting the file add following 2 lines
HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders();
Aug 17, 2012 06:37 AM|LINK
Santosh it gives Server cannot clear headers after HTTP headers have been sent. when i called it after flush()
120 Points
21 Posts
Aug 17, 2012 09:12 AM|LINK
From which method u have read the data1 for the file.
If u have done this using any stream reader than try to close that stream reader and dispose it.
This is the only cause u are getting the error.
282 Points
241 Posts
Aug 17, 2012 09:19 AM|LINK
hi first close the file its opened. thats why it will give that error.
File.Close() method after delete your file then it will be working..
System.IO.File.Delete(XlsPathAndFilename); //Line of Error
All-Star
42885 Points
7019 Posts
MVP
Aug 20, 2012 09:08 AM|LINK
Hello,
EhsanMubeen System.IO.File.Delete(XlsPathAndFilename); //Line of Error HttpContext.Current.Response.End();
Interchange the above lines and see again (A.k.a. End the response before deleting)
HttpContext.Current.Response.End(); System.IO.File.Delete(XlsPathAndFilename); //Line of Error
Please 'Mark as Answer' if this post helps you.
Participant
1763 Points
534 Posts
Aug 20, 2012 09:27 AM|LINK
try this.
string filePath = "~/images/add-edit.jpg"; Response.ContentType = "image/jpg"; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); Response.TransmitFile(Server.MapPath(filePath)); Response.End();
after download.. u can delete the image.
Thanks.
EhsanMubeen
Member
433 Points
100 Posts
Delete File after Download
Aug 17, 2012 06:04 AM|LINK
Hi All,
i am having trouble when attempting to delete file after download
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + XlsFilename);
HttpContext.Current.Response.OutputStream.Write(data1, 0, data1.Length);
HttpContext.Current.Response.Flush();
System.IO.File.Delete(XlsPathAndFilename); //Line of Error
HttpContext.Current.Response.End();
opens SaveFiledialog File.Delete gives "Used by another process error". How do i know that user has downloaded the file or cancelled the dialog ??
Mark As Answer if my reply helped you
Pray|Game|Code - My Way of Living
santosh.jagd...
Star
7625 Points
1454 Posts
Re: Delete File after Download
Aug 17, 2012 06:23 AM|LINK
before deleting the file add following 2 lines
MCP
EhsanMubeen
Member
433 Points
100 Posts
Re: Delete File after Download
Aug 17, 2012 06:37 AM|LINK
Santosh it gives Server cannot clear headers after HTTP headers have been sent. when i called it after flush()
Mark As Answer if my reply helped you
Pray|Game|Code - My Way of Living
v.vyas
Member
120 Points
21 Posts
Re: Delete File after Download
Aug 17, 2012 09:12 AM|LINK
From which method u have read the data1 for the file.
If u have done this using any stream reader than try to close that stream reader and dispose it.
This is the only cause u are getting the error.
Jr. Web Developer
India
venkimca
Member
282 Points
241 Posts
Re: Delete File after Download
Aug 17, 2012 09:19 AM|LINK
hi first close the file its opened. thats why it will give that error.
File.Close() method after delete your file then it will be working..
System.IO.File.Delete(XlsPathAndFilename); //Line of Error
Ruchira
All-Star
42885 Points
7019 Posts
MVP
Re: Delete File after Download
Aug 20, 2012 09:08 AM|LINK
Hello,
Interchange the above lines and see again (A.k.a. End the response before deleting)
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.shivanand G ...
Participant
1763 Points
534 Posts
Re: Delete File after Download
Aug 20, 2012 09:27 AM|LINK
try this.
string filePath = "~/images/add-edit.jpg";
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
after download.. u can delete the image.
Thanks.
shivanand.G.N (shivu.betta@gmail.com)