[英]Chrome - How to display PDF file from server and delete from server after browser displays file
I have an ASP.NET web api app using JavaScript to open a link to a PDF file on a server. 我有一个使用JavaScript的ASP.NET Web API应用程序,用于打开服务器上PDF文件的链接。 After opening I delete the file from the server.
打开后,我从服务器删除文件。 Using Firefox, Safari and IE I am able to see the file on the browser even after the file is deleted.
使用Firefox,Safari和IE,即使删除了文件,我也可以在浏览器上看到该文件。 When I run the function in Chrome I see no file and a 404 error displayed.
当我在Chrome中运行该功能时,没有看到文件,并且显示404错误。 How can I make this functionality work in Chrome as well?
如何在Chrome中也能使用此功能?
Put Method in C# to delete PDF 在C#中放入方法以删除PDF
public void Put([FromBody]string filepath)
{
string pdfGUID_DIR = filepath.Split('/')[0]; //get the first part of the dir which is the folder name (GUID value)
string tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.Configuration.ConfigurationManager.AppSettings["TemporaryDirectoryParent"], pdfGUID_DIR + @"\");
try
{
Directory.Delete(tempDir, true);
}
catch (IOException)
{
Directory.Delete(tempDir, true);
}
catch (UnauthorizedAccessException)
{
Directory.Delete(tempDir, true);
}
}
JS Code to Open PDF JS代码打开PDF
window.open('/Report/' + pdfURL); //Link to PDF file on server
setTimeout(removePDF(pdfURL), 1000);
JS Function with AJAX call to delete PDF 带有AJAX调用的JS函数删除PDF
function removePDF(pdfURL) {
$.ajax({
url: '/api/pdf',
type: 'PUT',
data: "=" + pdfURL,
});
}
If you're not using MVC (even if you are, but you need to alter the ActionResult for that). 如果您不使用MVC(即使您使用的是MVC,也需要为此更改ActionResult)。 Post the file as a Response and after you posted it, just dump it.
将文件作为响应发布,并在发布后将其转储。
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename));
Response.WriteFile(fullpath);
Response.End();
// dump file here: like
System.IO.File.Delete(fullpath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.