Image Not Displaying when the path is Temp Folder in Asp.net - asp.net

I am getting the content of Image in byteArray format from the database and displaying it in the Content image tag in ASP.NET.The problem is that when I save the image file in a specific location in my working folder and display the image,it is working fine.But if I try to save the image in a temporary folder and pass the temporary folder URL to image tag it doesn't display the image.
code:
Bitmap bi = new Bitmap(byteArrayToImage(FileUpload1.FileBytes));
//This code working fine
string path = Server.MapPath("Images/") + FileUpload1.PostedFile.FileName;
bi.Save(path , ImageFormat.Jpeg);
Image1.ImageUrl = "Images/" + FileUpload1.PostedFile.FileName;
//This code didn't displaying the image.
bi.Save(Path.GetTempPath() + FileUpload1.PostedFile.FileName, ImageFormat.Jpeg);
Image1.ImageUrl = Path.GetTempPath() + FileUpload1.PostedFile.FileName;
what is the problem with the tempFolder here?.I am using the Windows7 OS.Here i am using the temparory folder for automatically deleting the created images from the folder.
Thank you

Did you debug and check if the ImageUrl is correct

Related

ASP.NET C# image file not get in folder

string filename = bupload.PostedFile.FileName; //upload pdf file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(filename);
System.Drawing.Image img = System.Drawing.Image.FromFile("watermark.jpg");// upload image
pdf.Pages[0].BackgroundImage = img;
pdf.SaveToFile("watermark.pdf");
how can add watermark and save a file in particular folder in my website and no change he name of file file name is same as uploaded file name

how to send an image via email without saving it to a folder before

I am uploading images to an asp.net website using fileupload and i need to send the uploaded images via email to do this i am saving the image to a folder on the erver and then i am sending the image but can i send it without saving it to a folder before or if i need to save it to a folder can i delete it as soon as it's sent?
thanks.
my code:
string filename = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("UploadImages/" + filename));
string attachmentPath = Server.MapPath("UploadImages/" + filename);
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentType.Name = Path.GetFileName(attachmentPath);
mail.Attachments.Add(inline);
using System.Net.Mail;
[WebMethod]
public void SendFile(HttpPostedFileBase file)
{
//...setup mail message etc
Attachment attachment = new Attachment(file.InputStream, file.FileName);
message.Attachments.Add(attachment);
}

I am not able to save image into Folder

I am trying to save a image in specified folder but it doesn't saving image but code is working for excel file storing...did i missed something here....please suggestions needed.
string filename = Path.GetFileName(imgUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Files/");
imgUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
string filePath = Path.Combine(folder, filename);
imglogo.ImageUrl = Server.MapPath("~/Files/" + imgUploadControl.FileName);

why is the image icon blank/white when fileupload.saveas

i use vb.net
i trying to do a file upload, i want to image to save to image folder, however, the image don't know appear in the directory that i indicate
if i click on "show all file", the image appear, but the image icon is blank or white like the image below show
so i click on that image and click on "include it in the project" , however, it shouldnt be the case that i everytime upload an image, i need to redo that again
so how should i allow don't appear the white icon and to always appear in the upload folder when i upload a image instead of manually click on the image to include in ?
this is my code
Protected Sub uploadImage()
Dim filename As String = FileUploadImg.FileName
Dim fileType As String = filename.Substring(filename.Length - 4).ToLower()
If (fileType = ".gif") Or (fileType = ".jpg") Or (fileType = ".png") Then
FileUploadImg.SaveAs("C:\Users\Jane\Desktop\project\FileUpload\FileUpload\WebRole1\images\" & txtboxName.Text & "_" & FileUploadImg.FileName)
Else
MsgBox("failed")
End If
End Sub
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
uploadImage()
'End If
End Sub
this is the image of how it look like
You have to save posted file to persisted storage on the web server and map files to some URL so they can be accessed from the internet.
In Azure environment local disk (like c:) is not persisted (imagine multiple web-roles - how could you serve same image from other instances).
Solution is Azure Blob Storage (you have to set-up it in you Azure Management Console) and upload posted file to the Blob Storage Container.
// Setup the connection to Windows Azure Storage
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var blobClient = storageAccount.CreateCloudBlobClient();
// Get and create the container
var blobContainer = blobClient.GetContainerReference("public-images");
blobContainer.CreateIfNotExist();
// Allow blob to be downloaded
containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
blobContainer.SetPermissions(containerPermissions);
// Get the target blob reference
var blobAddressUri = String.Format("{0}{1}", fileName); //create random fileName here
var blob = BlobContainer.GetBlobReference(blobAddressUri);
// Set blob Content-Type
blob.Properties.ContentType = FileUploadImg.PostedFile.ContentType ;
// Upload to the blob storage account
blob.UploadFromStream(FileUploadImg.FileContent);
Your file is now available at blob.Uri.
You have to designate a directory where your uploads will be placed. For example, if I create a new project in C:\, Website1, the direcotry would be C:\Website1. I'd then create a directory under to store uploads: C:\Website1\uploads and make sure to set permissions to read/write for IIS account. I'd then do:
string uploadPath = MapPath( "pictures\\" );
this.fileUpload1.SaveAs( uploadPath + this.fileUpload1.FileName );
Once the file is in the upload directory, I can move it anywhere I like.

viewing the images problem

Using ASP.Net and VB.Net
Code
Image1.ImageUrl = "c:\Blue hills.jpg"
The above code is not viewing the image when i clicked the button.
How to solve this problem.
Need Code Help
You shouldn't be referencing the local file path - you should set the ImageUrl to be the URL on the webserver - for example, if you had the file in the root of your website, you'd reference it as:
Image1.ImageUrl = "/Blue%20hills.jpg"
The value you are supplying to the ImageUrl property is a file path and not a Url.
Ideally you should have a folder in your ASP.NET application that holds your image files which you can then reference with either a relative or absolute Url path i.e:
Image1.ImageUrl = "/Content/Images/Blue hills.jpg"
If you want to show pictures from the server disk that are outside of the website root, you have to build "proxy" page. For example call it ShowImage.aspx and it will get the picture name on the URL.
Then you'll have such code
Image1.ImageUrl = "ShowImage.aspx?image=Blue hills.jpg"
And the code for ShowImage.aspx code behind:
string imageName = Request.QueryString["image"] + "";
if (imageName.Length == 0 || imageName.Contains("/") || imageName.Contains("\\") || !imageName.ToLower().EndsWith(".jpg"))
{
throw new Exception("Invalid image requested");
}
else
{
string strFullPath = "C:\\" + imageName;
Response.ContentType = "image/jpeg";
Response.Clear();
Response.WriteFile(strFullPath);
}
Response.End();
This is C# but should be simple enough to translate into VB.NET as well.
Edit: added basic validation that verify that user does not try to fetch files from different directory and allows only .jpg files.
Image1.ImageUrl = System.Web.HttpContext.Current.Server.MapPath("~") + "/"+ "Images" + "/" + "Blue hills.jpg";
but as said before you can't reference a local file like this , except u use HttpHandler.
http://www.15seconds.com/issue/020417.htm

Resources