Embed Image to aspx page from code behind - asp.net

I'm programmatically (server-side) creating an Image object after my user chooses some value from a combonox.
I want to embed this image back into the aspx page at runtime, without saving it to disk or database.
So how can I do this? I tried to use memory stream, and send the image with response object, but than I only saw the image - it had overwritten the entire page.
Thanks...

Create the Handler and request it via:
<img src="Image.ashx?ID=myImageId" alt="text here"/>
<asp:Image runat="server" ImageUrl="~/Image.ashx?ID=myImageId" />

You could base64 encode the data and write it to the image data.
Like this:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
For this you will have to render the img tag yourself.
This way you don't have to write your image to a file on the server. It is sent from memory directly to the client browser.
Refer: Data URI schemes
Note this does not work with IE6.

Do something like this
<img src="yourimagegeneratingpage.aspx?query=value" />
When this image will be loaded it will generate a request to your page (yourimagegeneratingpage.aspx) and that page would respond with the image which will be shown by this img control.

Related

Open image on next aspx page

I show image thumbnails on page, Now I want when i click on image thumbnail the big images related to that thumbnail open in IamgeDispData.aspx page where i created a data entry form in that page to enter data related to that picture. How can I do this? With help of query string or else??
Query String is a good option.
What you can do put your image inside a asp:hyperlink
Set the URL of asp:hyper link with querysting
And in your detail page get it from url
Note that you can only pass the filepath in the url
And you will have to show it from the physical location
With help of query string ??
Yes, you can use it. Just surround your image with <a> anchor tag or use asp:hyperlink server side tag.
It can be something like
Content/Thumbnails/page.aspx?imageName="your image name"
e.g.
<a href="http://www.espn.com" target="_blank">
<img src="ahman.gif" />
</a>
Ref:
html - image links
Hyperlink image
How to control the image size of a Hyperlink?
example code snippet:
<asp:HyperLink runat="server" ID="hlThumbnail" NavigateUrl='<%# Eval("Url") %>'
Target="_blank" Style="height: 66px;">
<asp:Image runat="server" ID="imgThumbnail" Height="66px"
ImageUrl='<%# Eval("Thumbnail") %>' />
</asp:HyperLink>
In case you have no issue in showing Id to user , it will be the best solution. In case you have some issue in showing Id , you can encrypt it and then use it as Id. You can also validate then user again in the ImageDispData.aspx if he has the permission to enter the data or not for the image.
I am giving way to use using querystring and javascript.
You can use window.open function of JavaScript to open in new window or tab. When you are generating the thumbnail image , create them with url like : yourBigimage.aspx?photoId=10 , open this url using jquery onclick event of image and use window.open

Avoid browser cache in asp.net

I try to disable the html url browser cache using programmatically.
I am developing as a site using asp.net, I need to disable the html url browser for the security reason.
I tried many ways to disable the cache but none seems to work. Any ideas?
<iframe id="iframe" src="http://www.phy.mtu.edu/basiccomputing/sample.html" runat ="server" width="200" height="300"></iframe>
As far as it's a html file you want to work with, a easy solution is to append a random number and the end of your url like this
<iframe id="iframe" src="http://www.phy.mtu.edu/basiccomputing/sample.html?12345" runat ="server" width="200" height="300"></iframe>
I don't know wich view engine you are using, so i cant provide a sample you simple copy and paste.
You have to replace ?12345with a random number like new Random().Next().ToString()
If you call a asp.net page you can control it with
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
more informations about that:
ASP.NET Cache Examples
hope this helps
You could attach a random parameter to the URL so the browser thinks it's a different page every time.
http://www.phy.mtu.edu/basiccomputing/sample.html?rand=[INSERT RANDOM HERE]
try
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)). This site has something that can be quite helpful.

HTML File Upload in ASP.NET: HttpPostedFile Attributes Are Empty

In my ASP.NET web form, I'm trying to customize my File Upload panel. What I do is simply put an HTML input button and a textbox. And there's a hidden HTML file upload control. When user clicks on the button, file select window appears, when user selects the file, the value is written to the visible textbox.
It's all good so far. But I'm having a problem while trying to attach the selected file to email in code page.
Here's the HTML markup:
<asp:ImageButton ID="clickme" runat="server" ImageUrl="Images/browse.png" OnClientClick="$('#uploadme').click(); return false;" />
<input id="uploadme" name="uploadme" type="file" style="visibility: hidden;" onchange="CopyMe(this, 'txtFileName');" />
<input id="txtFileName" type="text" name="txtFileName" readonly="readonly" class="file-path" />
And the JS (I use this in order to copy the file name only and write it into txtFileName just to show to user):
<script type="text/javascript">
function CopyMe(oFileInput, sTargetID) {
var arrTemp = oFileInput.value.split('\\');
document.getElementById(sTargetID).value = arrTemp[arrTemp.length - 1];
}
And the CSS:
input[type=file] { width: 1px; }
I'm using the below code in my .cs file to get the file attributes:
HttpPostedFile file = Request.Files["uploadme"]
But I kept on getting null value. So after some research, I've learnt that my form has to use enctype="multipart/form-data" property. Since my .aspx file is under a master page file, I added this property to the form in my master page file. Now Request.Files["uploadme"] is not null but its file name is empty string and its ContentLength is 0.
I'm unable to understand what might be the source to this issue. If it's because of using Master Page's form, I can't add a form to my child page because it says a page can have only 1 form. I don't know if I could use JavaScript for uploading because I need to email the file after uploading so I don't know how to get the file after I upload via JS.
How can I solve this problem? It could be one way or another, all I need is a stylized upload panel and to e-mail file after uploading.
You're using a master page and I'm presuming that you are using .net 2.0 or greater. If that's the case, use a FileUpload control (you can still hide it using CSS). Using the FileUpload control means you won't need the enctype attribute (although you shouldn't generate any problems having it there).
This all being said, I don't believe to you copy values into file type fields and have documents upload. This would be a MAJOR security flaw as a website could potentially upload files from your machine without your knowledge.

Refresh an iframe using ASP/VB?

I have an iframe, looks like this:
< iframe id="PDFLetter" src="http://127.0.0.1/letterwriterasp/pdfs/test.pdf" width="60%" height="500" runat="server" scrolling="auto" >< /iframe>
Problem is, that pdf file is regenerated. So I need to refresh the iframe to reflect the changes to the user. I tried this:
PDFLetter.Attributes("src") = ""
PDFLetter.Attributes("src") = "http://127.0.0.1/letterwriterasp/pdfs/test.pdf"
But to no avail. It doesn't refresh the pdf in the iframe.
Not sure what can be done here. Any ideas?
Thanks,
Jason
Are you doing this on a postback and entering the code in the PageLoad event? If so it should work (for complete control over html you can laways just write to a Literal control although this is much heavier).
Try adding a random number at the end of the pdf name using ?randomnumber so that it does not use a cached version of the pdf file.

How to show Image with http url in asp.net

In sql server database, I have column name Image which has urls like http://www.xyz.com/1009/image1.jpg
Now I want to display it on my datalist. But it is not showing any image. It is might be the case because it is expecting the Image URL to be ~/Folder/abc.jpg
Then how to show the image on the asp.net page when I have image URL, which control I need to use and how?
Do you see little red X instead of an image?
If so, right click it --> Properties (IE) to see the URL it's referring to.
Might give us hint on what's going on. :)
If you are getting the image from the database in for the form xzy.com/1009/image1.jpg but want to add the http:// then your code should be
<asp:Image ID="imgmain" runat="server" ImageUrl='<%#String.Format("http://{0}", Eval("Image"))%>' Width="80" Height="80" />

Resources