0

I have an ASP.NET web api app using JavaScript to open a link to a PDF file on a server. 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. When I run the function in Chrome I see no file and a 404 error displayed. How can I make this functionality work in Chrome as well?

Put Method in C# to delete 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

 window.open('/Report/' + pdfURL);  //Link to PDF file on server
 setTimeout(removePDF(pdfURL), 1000);

JS Function with AJAX call to delete PDF

  function removePDF(pdfURL) {
        $.ajax({
            url: '/api/pdf',
            type: 'PUT',
            data: "=" + pdfURL,
        });

    }
Vahe Jabagchourian
  • 1,278
  • 3
  • 20
  • 62
  • Why do you send the delete method from the client? You could "serve" the file to client and do with the file whatever you want afterwards (rename or remove) - serverside that is – Leon Sep 18 '14 at 18:49
  • Hi Leon, thanks for the reply. It appears that my timeout delay was set too low. I now see the file and after the timeout it deletes when the timeout triggers. But, in response to your comment, how do I delete the file after I serve it? I do not want to keep files on the server. I am using a pdf generator (wkhtmltopdf) that saves files on the server and then I serve the file to the client. – Vahe Jabagchourian Sep 18 '14 at 18:53
  • Don't depend just on a timer. Everyone with a slow internet connection would have a problem. (This isn't just chrome).. I don't know *how* you actually serve the file, but I'm creating an actual response here... – Leon Sep 18 '14 at 18:56

1 Answers1

0

If you're not using MVC (even if you are, but you need to alter the ActionResult for that). 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);
Leon
  • 909
  • 6
  • 21
  • using MVC > try this answer: http://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult – Leon Sep 18 '14 at 19:06
  • Thank you Leon. In my Post method within the Controller I currently run an executable file called Wkhtmltopdf.exe. It creates the pdf on the server folder. I then return the pdf file location from the Controller and have my JS function open the created file on the directory accessible from the browser. I will try the approach you have provided. – Vahe Jabagchourian Sep 18 '14 at 19:06