my web page is uploading images to server folder which is 'd:\upresim'
I adding images with that code:
protected void Button2_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/image/a.png"));
Image1.ImageUrl = "~/image/a.png";
}
I have a selected image, that I receive from FileUpload that I have added from the selected image to server upresim into folder.
Afterwords I need to show the added image on Image1, but it show nothing what can I do?
You have to upload images under your web app folder, for that folder your app has to have rigths to write to filesystem, and then set relative url to Image control ImageUrl. You should also check if upload has a file, and preferably use file name from uploaded file.
For example, let's say that you have folder upresim in your web site root folder, then use this code :
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("/upresim/") + FileUpload1.FileName);
Image1.ImageUrl = "/upresim/" + FileUpload1.FileName;
}
Related
Using AJAX 4 (latest version) I have been working with the html editor extender trying to upload images with text, I have got the Image to upload however it appears blank and when looking at the source, the source of the image is also blank (image below) how do I resolve this upload my selected image?
Include in HtmlEditorExtender an event handler for the ImageUploadComplete event.
<ajaxToolkit:HtmlEditorExtender
OnImageUploadComplete="MyHtmlEditorExtender_ImageUploadComplete"
...
Within the ImageUploadComplete event handler, you need to do two things:
1) Save the uploaded image
2) Provide the URL to the saved image so the image can be displayed within the HtmlEditorExtender
protected void MyHtmlEditorExtender_ImageUploadComplete(
object sender, AjaxFileUploadEventArgs e)
{
// Generate file path
string filePath = "~/Images/" + e.FileName;
// Save uploaded file to the file system
var ajaxFileUpload = (AjaxFileUpload)sender;
ajaxFileUpload.SaveAs(MapPath(filePath));
// Update client with saved image path
e.PostedUrl = Page.ResolveUrl(filePath);
}
Make sure you checked
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/HTMLEditorExtender/HTMLEditorExtender.aspx and
http://stephenwalther.com/archive/2012/05/01/ajax-control-toolkit-may-2012-release
I am using ajax toolkit from here
I can upload .jpg, .jpeg, .txt, .doc, but not able to upload .wmv file..ie not able to upload videos file.
Below is my code
File.aspx
<asp:AjaxFileUpload ID="AjaxFileUpload1" OnUploadComplete="AjaxUpload1_OnUploadComplete"
runat="server"/>
File.cs
protected void AjaxUpload1_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
// Generate file path
string filePath = "~/Images/" + e.FileName;
// Save upload file to the file system
AjaxFileUpload1.SaveAs(MapPath(filePath));
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", string.Format("<script type='text/javascript'>alert('{0}')</script>", ex.Message.ToString()));
}
}
I want to know where i am wrong... what i have to add in the code so that i can upload video files
Hey Pritesh have you check your file size.
by default 4 mb file upload avail in asp.net
If problem of size then add below line in Web config file.
<system.web>
<httpRuntime maxRequestLength="11264" />
</system.web>
value change as per ur req.
my question is that how can i immediately display the server-side uploaded image [image being uploaded at a button click] ?
upload done using fileupload control.
image retrieval expected using image control.
below is the part of my code.
protected void Button1_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
FileUpload1.SaveAs(Server.MapPath("uploaded images/"+FileUpload1.FileName));
}
this code successfully uploads the image to "uploaded images" folder in the server.
but i failed to retrieve it to display as soon as it is uploaded.
below is what i tried, which never gave any programming error, though it never gave the
result too!
Image1.ImageUrl = (System.IO.Path.GetFileName(FileUpload1.FileName)).ToString();
help expected.
regards.
You really shouldn't assign local pathname as source for uploaded image.
Try something like
Image1.ImageUrl = "uploaded images/"+FileUpload1.FileName;
string imgPath = this.ResolveUrl("~/uploaded images/" + FileUpload1.FileName");
Image1.ImageUrl = imgPath;
Here's a full discussion of ASP.NET and image path issues.
I have image control.I want to load image from my specific path.
i have a code in page behind
string imagePath ="E:/DotNetProjects/Templates/Default/icons/Computer.png";
imgEditor.ImageUrl = imagePath;
imgEditor.AlternateText = "Unable To Find Image";
path is exist and image is also available but always load alternate text.
imgEditor is my image control ID.
Plz help to catch my mistake.Thanks.
Just put your image in solution(any folder or even in root) and path image uri from that (with src in asp page) like :
src="Templates/Default/icons/Computer.png"
The imagePath is a filesystem path... you need a URL... (something like http://...). The URL must be accessible from the browser i.e. you need to setup your webserver (IIS) to serve the respective path... I would recommend putting the image into the solution/project so that the URL is relative...
I have a folder C:\Images which has a some images. This folder is not inside my project and I would to know if there is a way to load an image from that folder on to an ASP.NET Image control.
<asp:Image ID="img" runat="server" />
One solution could be to make the Images folder a Virtual directory on the IIS but I would like to know if this can be done without creating a virtual directory for the Images folder.
Assuming proper access is granted to the Images folder you could do something like this:
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
mainImage.ImageUrl = "ImageHandler.ashx?image=MyImage.jpg";
}
ImageHandler:
public void ProcessRequest(HttpContext context)
{
byte[] imageBytes = File.ReadAllBytes(#"C:\Images" + context.Request["image"]);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageBytes);
}
At the end of the day, the image needs to be sitting somewhere that the user's browser can see it.
That means your options are:
Move the image to a directory on the webserver
Set the image's directory up as a Virtual Directory
Copy the image at runtime to a directory on the webserver
The Best Practice answer is to put the images into your project if they're part of the site, or to map a Virtual Directory if the directory is a store of user-uploaded images.