ItextSharp, Save Pdf Without Displaying Open / Save Dialog Box - asp.net

Is it possible not to show Open/Save dialog? I would like to save the pdf file straight to specific disk location on client PC.

Yes, you can write the file directly to disk without the use of a save dialog. All you need is a directory path to write too.
string path = "C:\YourDirectoryPathHere";
System.IO.File.WriteAllBytes(Path.Combine(path, "NameYourFile.pdf"), myPDF);
This assumes myPDF is a byte[].

Related

mPDF to output a local file + file inline to the browser

I wanna know if i can output a pdf, using mPDF, to a destination as well as a preview in the browser at the same time. ie. I+F at the same time.
I: send the file inline to the browser. The plug-in is used if
available. The name given by filename is used when one selects the
"Save as" option on the link generating the PDF.
F: save to a local
file with the name given by filename
My current code is like this:
$mpdf->Output($OutputLocation,F);
$mpdf->Output();
exit;
It can export the pdf file to $OutputLocation successfully, however, in the browser, it shows:
mPDF error: Some data has already been output to browser, can't send
PDF file
In the documents of mPDF, it has only show one option for one time, any methods to do both in the same output process?
http://mpdf1.com/manual/index.php?tid=125
Please help, thanks.
It turns out that it is an issue on QNAP that their default php.ini is not allowing output buffering.
To solve this, I have edited the php.ini in the WebUI and change the output buffering to 4096. Then it works again.
output_buffering = 4096

Read from bytestream in asp

I'm wondering if it's possible to read from a bytestream without saving it.
I have a file, wich is a PDF that's in a mailbox. Through code I can access this mailbox and read out the attachements in it (the pdf's) At that point every pdf is in a bystream.
For now I just save it locally to test if my code works. But I need to work without saving the file.
Is there a way so I can get data from that file without saving. I need to process the first page and find the image in there (which is a qr code).
I have code to do this from a local file, but I want this to be directly from on of those in the bystreams.
Can I store the bytesteam in something like an object or a list of bytestreams and use that?

Saving a generated html document to specific folder in ASP.NET

So I have written a simple program that creates an html document based on user input into a number of fields. Currently, when the user pressed a button it generates the document and automatically downloads onto the user's machine using the following:
Response.ContentType = "text/plain";
Response.Write(HTML.ToString());
Response.Flush();
Response.End();
But, I would like to have the file written to a folder on the user's machine when a user presses a button. So, let's say I want them to press the button and it automatically writes the file to their desktop. What would this code look like? (c#)
Thank you,
p.s. I have been trying something like this with no luck:
string filename = Server.MapPath("~/C:/Users/Sean/Desktop/new.html");
System.IO.StreamWriter textWriter = default(System.IO.StreamWriter);
textWriter = System.IO.File.AppendText(filename);
textWriter.Write(HTML);
textWriter.Close();
Using ASP.Net, you CANNOT force user to save any file on any specific location. Moreover the file you want user to save is HTML which will be rendered directly on browser i.e. user will not be prompted to save it on their machine.
So to answer your question, you cannot have ANY file automatically saved to user's machine. There has to be some manual intervention by the user(to select the path at which he/she wants to save it.)

asp.net image jpeg not saving correctly

I am trying to save a jpeg image in an uploads folder which has correct permissions setup. When I test the file is being saved (eg: images/uploads/Winter.jpg) but if I try to view the image in my browser or if I attempt to open the image using anything else the image does not display.
I think that the file is not being encoded correctly before saving it to disk but am not very experienced dealing with the saving of files, encoding. Does the below code look ok or do I need to encode the file being uploaded somehow before saving it to disk?
String imgPath = "newsletter\\images\\uploads\\";
String filename = System.IO.Path.GetFileName(upload.PostedFile.FileName);
filepath = imgPath + filename;
filepath = Request.PhysicalApplicationPath + filepath;
upload.PostedFile.SaveAs(filepath);
The file saves to the correct folder but is only 150bytes in size. If I try to browse to the file and view it with an image viewer it does not display correctly.
Encoding shouldn't be a problem - the raw data isn't changing. However, it's possible the browser isn't sending all the data, or that the upload control is deleting the data before you're saving it.
Make sure that you call .SaveAs() before the page begins unloading, and before any additional postbacks. I think we'll need to see more surrounding code to help further.
Another note - by allowing the existing file extension to be used, you're allowing users to upload .aspx files, which could subsequently be executed through a request. Safe filenames are GUIDs and whitelisted file extensions. Using un-sanitized uploaded path information is very dangerous. If you re-use filenames, sanitize them to alphanumerics.

Asp.net creating tab delimited txt file

I have to create a tab delimited txt file from a query.
I want to call an HttpHandler that returns my txt file as a stream, I don't want to create the file phisically.
1st question:
what is the best practice to create the tab delimited txt file from a query result?
I have to fetch all rows and create the file manually?
2nd question:
How to set a timeout for the HttpHandler that creates the file?
Thanks for your time.
I would create a plain old http output stream and change the content type to 'text/plain' which means that you don't need to physically create the file on the web server, and if you add the content-disposition header to the output and specify that it has an attachment called something like 'report.txt' the user will be prompted to Open or Save the content, rather than just viewing it in the browser like a normal web page.
You can use the Server.ScriptTimeout = x to set the script timeout by gaining access to the current HttpContext object
Hope this helps

Resources