C sharp Text to jpg - how to change image extension - c#

I am using following code to convert text to jpg on the fly.
context.Response.ContentType = "image/jpeg";
SaveAsJpeg(bitmap, context.Response.OutputStream, (long)100);
When I display the page on the internet images look fine on the surface. But when I right click and save image as or save page as, I get ashx as file extension.
How can I get jpg as file extension?

context.Response.AddHeader("Content-Disposition", "attachment; filename=your-name-here.jpg");
This tells the browser what name the file should be saved as.

Related

ITextSharp - Google Chrome Save Button Saves PDF as .ASPX page

I am currently using ITextSharp to create a PDF using a .Net 3.5 ASPX page. Everything works well.. the only issue is that in chrome(Version 42.0.2311.135) when I click the save button or right click and say save as it tries to save as an .aspx page. I don't experience these issues when using Firefox or Internet Explorer. I tried disabling the chrome PDF viewer and it just automatically saves the file as an aspx file. I've included my code below which is fired from a button click. I am assuming it is the way I am handling the response? Any ideas?
iTextSharp.text.Document d = new iTextSharp.text.Document(PageSize.A4, 20, 20, 20, 20);
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
w = PdfWriter.GetInstance(d, stream);
//add stuff to itext document
d.Close();
byte[] b = stream.ToArray();
stream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(b);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
UPDATE:
Wanted to share my findings in case it saves someone the headache. Adding Response.AddHeader("Content-Disposition", "inline;filename=myfilename.pdf"); fixed most of the issues but I was still experiencing a problem with the save button in the Google chrome viewer(print worked fine but save prompted the user to save the page and not the pdf). The PDF is being generated on a .aspx page through a button click. After it is generated I clear the response and dump the PDF to the page. The form element on the page didn't have a method set so it was being sent as post(found this out through fiddler). This page was being shared through an iframe. I changed the method to GET on the page and everything is now working as expected.
Try with:
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.pdf");

display *doc,*xls,*pdf in browser window instead

i want to display *doc,*xls,*pdf in browser window instead of their respected application. i have tried following code, but no luck it prompt me dialog for save/open instead of displaying in browser
//Set the appropriate ContentType.
Response.ContentType = "Application/msword";
//Get the physical path to the file.
string FilePath = MapPath("wordfile.doc");
//Write the file directly to the HTTP content output stream.
Response.AppendHeader("Content-Disposition", "inline;filename=" + "wordfile.doc");
Response.WriteFile(FilePath);
Response.End();
please help
thanks
for any of these proprietary formats you need browser plugins/extensions or ActiveX for IE
Am not sure if its available for office formats.
For PDF setting the content type to "application/pdf" and Content-Disposition to inline should work.

Chrome PDF Viewer "Save To Disk" feature - is it possible to control saved filename extension?

In an ASP.NET 2.0 application, using Google Chrome 13 on Windows.
My app dynamically generates a PDF report when the user browses to a particular aspx page. For the most part, things work fine on various browsers.
However, on Chrome, when Chrome's PDF viewer is being used, the following can happen :
- the user presses the floating diskette icon on the bottom right hand side of the viewer to save the PDF document. The file is saved with the aspx page's name. e.g. Report.aspx .
If I open that downloaded aspx file in Adobe Reader, it opens up ok as a PDF document. But is there a way to get chrome to default the name of the file to save to have a ".PDF" extension ?
EDIT :
The code for the report generation page looks something like this :
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/pdf";
byte[] data = GenerateReportHere(); // dynamically generate PDF report
Response.AddHeader("Content-Length", data.Length.ToString());
Response.BinaryWrite(data);
Response.Flush();
Response.End();
}
Note that I don't want to use a "Content-Disposition" header like :
Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
because that causes the browser to ask the user if they want to download the file. In Chrome it won't display it in its PDF viewer - just gives the user the chance after downloading to open the file using whatever program they have associated with the ".pdf" file extension.
You should try using content-disposition header while streaming the PDF file to the browser. For example,
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=[ReportName].pdf");
// Code to stream pdf file content to the client
...
For more info about content disposition, see
http://www.jtricks.com/bits/content_disposition.html
http://greenbytes.de/tech/webdav/draft-reschke-rfc2183-in-http-latest.html
Since chrome is up to date with HTML5, we can use the shiny new download attribute!
Works

save image from fileupload

I am using fileupload control to have image on the form and that that image has to be saved on some folder in hard disk say "E:\\asp_net".
Is there any possible way to save image on disk from fileupload and if there is any other image with same name, it should be overwritten?
Why do you need to save the file in your machine's E drive?
Your ultimate option should be in your Application Folder. It should be like...
FileUpload1.SaveAs(Server.MapPath("~/AppFolderName/" + FileName));
You're looking for the cunningly-named SaveAs method.
following videos cover for your needs;
http://www.asp.net/general/videos/how-do-i-simple-file-uploads-in-aspnet
http://www.asp.net/general/videos/how-do-i-multiple-file-uploads-in-aspnet-1
http://www.asp.net/general/videos/how-do-i-multiple-file-uploads-in-aspnet-2
var filename = Path.Combine(#"E:\asp_net", myFileUploadControl.FileName);
if (File.Exists(filename))
File.Delete(filename);
myFileUploadControl.SaveAs(filename);

How to use Image conrtrol in asp.net (C# web)

I have an image stored in my MS Access database with the data type OLE Object. I want it to display in an Image control. How can I do this? I tried this, but only in a PictureBox control in windows forms. Please help. Thanks in advance.
You need to create a page that can read the image from the database as binary data, then write that data directly to http response as binary data using Response.BinaryWrite to feed the image out. Then the src attribute of your image is pointed at the page itself like:
<img src="image.aspx" />
And the code behind image.aspx:
// An assumed method to get binary data our of the database
var bytes = YourDataLayer.GetBinaryImageData();
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=filename.jpg");
Response.ContentType = #"image\jpg";
Response.BinaryWrite(bytes);
Response.End();
read the image from the database as byte array and then create a temperary image object and assign it to the webcontrol on the page

Categories