Reputation: 11
I am using asp.net to create a mobile web application to manage bike parts I have gotten the file upload working but now I need to figure out how to get an image control to display the image from its new location on a server.
I will be saving the image file path in a database I just need to figure out how to get that new file path for the image.
this is the code I am using for the file upload
if (this.FileUpload1.HasFile)
{
this.FileUpload1.SaveAs(Server.MapPath("~/Mobile/" + FileUpload1.FileName));
}
I can likely figure this out but just in case I can't figured I would post the question now than later as it can take a while to get an answer and I have a dead line
Upvotes: 0
Views: 1289
Reputation: 2012
You are going to have to use an "ImageHandler" to read the image properly.
This is how I did my handler.
public class ImageHandler : IHttpHandler
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dboBlog"].ConnectionString);
public void ProcessRequest(HttpContext context)
{
try
{
string messageid = context.Request.QueryString["mid"];
conn.Open();
SqlCommand command = new SqlCommand("SELECT Image from BlogMessages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn);
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
context.Response.BinaryWrite((Byte[])dr[0]);
conn.Close();
context.Response.End();
}
}
catch (Exception ex)
{
return;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
As you can tell, I use a QueryString. I use this querystring to call the image back. I call my image back in a gridview but this is how it looks...
<asp:Image ID="postImage" runat="server" ImageUrl='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>' Width="400px" AlternateText="No Image" ImageAlign="Middle" Visible="false" />
I do set the visibility to false because it's a blog and sometimes people don't upload an image. As you can tell, the image url calls the ImageHandler where the querystring is equal to the MessageID.
This works for me, so hopefully it will help you out.
Upvotes: 1