简体   繁体   English

Chrome-如何在浏览器显示文件后从服务器显示PDF文件并从服务器删除文件

[英]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.

相关问题 浏览器请求文件,服务器下载pdf,浏览器在iframe中显示pdf - Browser requests file, server downloads pdf, browser displays pdf in iframe 仅在浏览器中显示PDF一次,然后将其从服务器C#中删除 - Display PDF in a browser Only ONCE and delete it from server C# 如何从服务器中删除文件 - How to delete a file from the server 从服务器获取 pdf 文件,试图以 HTML 显示它 - Getting pdf file from server, trying to display it in HTML 从服务器删除加载的文件 - Delete loaded file from server 我想从服务器获取文件,然后使用 react js 在浏览器中显示(文件作为响应来自服务器) - i want to fetch file from server and then display in browser using react js(file is coming from server as response) 从服务器上的文件夹中删除特定的上载文件并显示剩余文件 - Delete specific uploaded file from folder on server and display remaining files 在客户端PC上下载pdf文件,并在开始下载后从服务器端文件夹中删除pdf文件 - download pdf file at client pc and remove pdf file from server side folder after download begin AngularJS:从服务器下载pdf文件 - AngularJS: download pdf file from the server 如何将pdf文件从前端发送到Nodejs服务器? - How to send pdf file from front-end to Nodejs server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM