Save a PDF that's been streamed to the browser - c#

I'm streaming PDFs to the browser, and it works fine. The problem is when someone clicks the save button. On the same server, we have a "Live" and a "test" site. They are identical, and I've verified that all code is identical. However I'm seeing different dialog boxes when the user clicks "Save"
Test site:
- dialog box: "Save A Copy"
- file name: "Document.pdf"
- file type: Adobe PDF
Live site:
- dialog box: "Save As..."
- file name: "Document.aspx"
- file type: Adobe PDF
To make things even more "fun" I've noticed this behavior ONLY with IE8. With Firefox or Chrome clicking save - regardless of which site (test or live) opens the "Save a Copy" dialog box which allows the users to save the file as a PDF instead of a .aspx file.
Any suggestions are appreciated

It might have to do with your IIS mime settings being different between the two servers.
Make sure your HTTP response headers are set properly as well. You'll be able to specify the filename using the content-disposition(?) attachment: filename.pdf (IIRC)

Related

Selenium C# Test with Chrome Headless - Chrome File Upload Window not working

I have a test which uploads a profile image to the web application. In order to upload the image file, an upload button is clicked which opens a file upload window as shown in the screenshot below.
The test then sends keys of the image file path to the file upload window in order to upload the file. When the test is run in chrome headless mode, the file upload window is not found and the send keys step fails.
If the test is run in normal mode (without headless option) the test runs fine without any issue. What could be the problem? Is there any way to fix this? Following code is used to set focus and send keys to the browser file upload window.
var dialogHWnd = FindWindow("#32770", "Open"); // Title for modal. IE: "Choose File to Upload"
var setFocus = SetForegroundWindow(dialogHWnd);
SendKeys.SendWait(#picPath);
Thread.Sleep(5000); //wait
SendKeys.SendWait(#"{Enter}");
Reporter.LogTestStepForBugLogger(Status.Info, $"Sent keys {picPath} and Enter to upload the Profile Picture.");
With selenium, you can just sent the full path of the image to the input element that is responsible for the upload.
Check the html code and search for relevant probably with "hidden" attribute or something common for this upload section.
Then just use:
driver.findElement(by.xpath('input xpath or id ..').sendKeys('path of the image in your system');

C# ASP.NET Retrieving file address in string

I am building a method that will build an XLS file and uploading it on user's computer.
I am using this guide:
http://csharp.net-informations.com/excel/csharp-create-excel.htm
So code that will define my destination address is:
xlWorkBook.SaveAs("C:\\Something\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal);
Now it is default, but i want to allow user to define it by him self, so as far as i understand, i need an html field, which will open common "browse window" and save file path to string, which will be later used in xlWorkBook.SaveAs function. I have read a bit about FileUpload, but i don't really sure that it is what i am looking for.
The code that you have there will save the file on the web server itself, not on the user's computer. You'll need to stream the file down to the user via the browser, and then they will be able to choose where to save it.
You could save the file on the server and then stream it to the user using Response.WriteFile, or you could stream it from memory if you don't want to keep a copy of the file on the server.
This code will create a file on the server, not on the users/clients computer. If you want the user to be able to download the file to his/her computer and select the location where the file is stored, you need to create a file (.aspx file or controller method, depending on wether you are using webforms or MVC) and have it stream the file to the user's browser. The browser will then take care of displaying the "Save as" dialog where the user can select the destination location.

ShellClass.BrowseForFolder(...) DialogBox not showing

I have a .aspx page in my project inside that there is one <a> link for download .txt files.
Am using Shell32.ShellClass and Shell32.Folder2 for showing Browse For Folder for ask user to Where do you want to save files instead it download on default Downloads folder.
Below is my C# code for showing DialogBox.
Shell32.ShellClass shell = new Shell32.ShellClass();
Shell32.Folder2 flder = (Shell32.Folder2)shell.BrowseForFolder(0, "Select destination folder", 0,"Desktop");
if (flder == null)
{
dlgResult = DialogResult.Cancel;
}
else
{
strPath = flder.Self.Path;
dlgResult = DialogResult.OK;
File.WriteAllText(strPath + "\\NewFile.txt", "file content abc tex...");
}
Problem: Above code working fine in Local but when I host website on IIS it's not working and not showing DialogBox.
Is there any specific Settings or Configurations in IIS for that ? or why it's not working when it host in IIS?
please give me suggestions.
Thanks.
Your C# code runs on the server, not in the web page. When you open a dialog box in this way, it runs in the context of the calling code. When running as part of IIS express (or any web server running as you), it opens in your Windows desktop because it is running as you. When running in IIS, it is running as a different account not attached to your desktop, so it will not show.
I am not aware of a way to invoke the "Save As" functionality to save a file to a different folder in JavaScript. The execCommand function may work but it varies from browser to browser. Some versions of Internet Explorer also limit what file types can be downloaded this way.

TransmitFile security issue in IE and word

I'm trying to transfer a file (word document) from my server to the client
I'm using this :
Response.TransmitFile(path);
Response.End();
In IE when I click open of the download popup, everything is ok and word open with my document.
The problem is when I click the save button in word, it pop me a security/connection popup. I have to give a username/password for mysite.com
That is normal behaviour since Word handles files opened via browser differently - it tries to verify whether they are "WebDAV-accessible" and uses some MS-specific extensions in doing so... that in turn doesn't use your current browser session but tries/needs to establish a new which in turn leads to Word askting you for credentials.
IF you want to just download it locally you could by using a content-disposition header - this way Word sees it as a local file and tries to save it locally upon "Word save button press".
IF you really want it to be saved back to your site it gets a bit tricky... although as you are running on IIS you might be able to implement something in combination with the IIS-built-in WebDAV functionality...
EDIT - as per comments:
For how to use content-disposition http header see for example http://www.jtricks.com/bits/content_disposition.html

Is there any way to make a PDF savable from inside the browser?

I have a file that I can get to with the UNC \mypath\myfile. I retrieve the path with c# and assign it to a link. The browser opens the file inside itself (ie display in the browser) without problem. But, when I try and save to \mypath\myfile I am prompted to save it locally. If I view the file outside of the browser (IE 7) I can edit and save as expected, again, via the UNC.
What I trying to do is use iframe to display the file from my UNC (file:///\mypath\myfile), which does work, but now I can't edit it. Outside the browser I can.
Is there anyway to save a PDF when displaying it inside the browser? I also tried a button to use the save method on the pdf, but it did not work.
Thank you.
I am using IE 7 and Adobe Professional 7.1.0.
When you open a file via a web browser the web browser downloads the file locally, then sends a local file path to the application that opens it. Even in the case you are using a UNC in your href Adobe does not get that UNC path to save back to, it is getting a local machine path. Keep in mind the browser does the same thing even if the UNC is a local machine path.
I don't think that the behavior you want is possible.
<body>
<div style="height:80px">
<p>Save
</div>
<div>
<iframe width="100%" height="100%" src="pdfname.pdf" />
</div>
</body>
On downloadpdf.aspx's page load event,add:
Response.AddHeader("content-disposition","attachment; filename=pdfname.pdf")
Response.ContentType = "application/pdf"
Server.Transfer("pdfname.pdf")
Untested, but should give you an idea...
Saving a PDF in your browser (through File-Save As in IE etc) or Save A Copy in the Adobe system will always prompt you to save it locally. You could navigate manually to your UNC path, but this is browser behaviour, not server behaviour.

Categories