从服务器下载文件,然后在服务器上删除
好的,我正在从服务器下载一个文件,我计划在客户端下载该文件后删除我在服务器上下载的文件。
我的下载代码工作正常,但我不知道何时输入删除命令文件。
string filepath = restoredFilename.ToString();
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
//Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
//// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
}
现在我知道response.End()将停止所有事情并返回值,那么还有另一种方法吗?
我需要调用一个函数
DeleteRestoredFileForGUI(restoredFilename);
删除文件但不知道把它放在哪里..我尝试放在 Response.End() 之前和之后,但它不起作用..
感谢任何帮助…谢谢
ok i am downloading a file from a server and i plan to delete the file that i have downloaded on the server after it gets downloaded on the client side..
My download code is working fine but i dont know when to put the command to delete the file.
string filepath = restoredFilename.ToString();
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
//Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
//// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
}
Now i know the response.End() will stop every thing and return the value, so is there another way too do so..
I need to call a function
DeleteRestoredFileForGUI(restoredFilename);
to delete the file but dont know where to put it.. i tried putting before and after Response.End() but it does not work..
any help is appreciated... thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在调用 TransmitFile() 之后添加
并放弃对 Response.End() 的调用(您不需要它)。
如果这不起作用,那么放弃 TransmitFile() 并继续:
Add
after the call to TransmitFile() and ditch the call to Response.End() (you don't need it).
If that does not work, then ditch TransmitFile() and go with:
您无法立即删除该文件,因为它可能尚未下载。从服务器端没有简单的方法可以判断文件是否已成功下载。如果浏览器打开打开/保存对话框怎么办?在确认对话框之前,下载不会开始。 (这可能不会立即发生和/或对话可能会被取消)
或者,如果它是一个大文件并且在完全下载之前断开连接怎么办?是否可以再次尝试下载?
通常建议的处理您的情况的方法是,在一段时间后将删除作为一个单独的过程进行,这样您就可以(相当)确定不再需要该文件和/或可以在需要时重新创建/恢复该文件。
根据您的情况,您可以有一个单独的进程来定期删除/处理旧文件。或者,如果流量较低,则可以在每次请求新文件时检查并删除旧文件。
旧文件的识别可能基于文件时间或数据库中的关联值。无论哪种方式,如果可能有大量文件需要处理,并且不太可能识别出大量要删除的文件,那么您不太可能需要频繁检查的开销。
另外,请务必解决大量文件未尽快删除的后果(磁盘空间真的是一个问题吗?),以防止可能在仍然需要时删除它们的副作用,或通过热心检查产生性能副作用。
you can't delete the file straight away as it may not have been downloaded yet. from the server side there is no easy way of telling that the file was successfully downloaded. what if an open/save dialog is opened by the browser? download won't begin until the dialog is acknowledged. (this may not be immediately and/or the dialog may be cancelled)
or, what if it is a large file and the connection is dropped before it is fully downloaded? should it be possible to attempt the download again?
the normally recommended way of dealing with your situation is to do the deletion as a separate process, after a time period which allows you to be (fairly) sure the file is no longer required and/or it can be recreated/restored if need be.
depending on your situation you could have a separate process which periodically removes/processes old files. or, if you have a low volume of traffic, you could check for and delete old files each time a new one is requested.
the identification of old files will likely be based on a file time or associated value in a darabase. either way, if there are potentially lots of files to process you are unlikely to want the overhead of checking very frequently if it is unlikely to identify a lot of files to remove.
also, be sure to way up the consequences of lots of files not being removed ASAP (is disk space really an issue?) against the side effects of possibly deleting them while still needed or creating a performance side effect by checking to zealously.
你所遵循的一般模式让我想知道,你在这样做吗?
如果是的话,您可能会将系统更改为在内存中工作。由于内存是在 .Net 中管理的,因此您不必进行手动清理,并且根据文件的大小,这也可能会快一些:
The general pattern you are following makes me wonder, are you doing this?
If you are, you might change your system to work in memory. Since memory is managed in .Net you wouldn't have to do this manual cleanup, and depending on the size of the file this could be a good bit faster too:
由于您在标头中设置了文件名,因此您有两个选择:
将文件内容读取为字符串、删除文件、回显/打印字符串作为消息正文。
将文件重命名为
delete-filename.xxx
之类的名称,然后使用一些外部进程(可能是 cron 作业?)来删除以该前缀开头的所有文件。Since you set the file name in the header, you have two options:
Read the file contents into a string, delete the file, echo/print the string as the body of the message.
Rename the file something like
delete-filename.xxx
and then have some external process (maybe a cron job?) that goes behind and deletes any files beginning with that prefix.