I have an upload interface where I upload images, the link to the image is saved in a column in Oracle, and Image is saved on the file system. I am having challenges viewing the image on my form.
Find below code snippets that handle saving image:
if (AsyncUploadFront.HasFile == false && AsyncUploadBack.HasFile == false)
{
divMsg.Visible = true;
lblMsg.Text = "Please select files to upload";
return;
}
if (!checkFileType(AsyncUploadFront.FileName))
{
divMsg.Visible = true;
lblMsg.Text = "Invalid file format. Allowed formats are png, jpg & gif.";
return;
}
if (!checkFileType(AsyncUploadBack.FileName))
{
divMsg.Visible = true;
lblMsg.Text = "Invalid file format. Allowed formats are png, jpg & gif.";
return;
}
var frontImage = string.Format("{0}_{1}_front{2}", txtAccountNo.Text.Trim(), dateString, frontExt);
var backImage = string.Format("{0}_{1}_back{2}", txtAccountNo.Text.Trim(), dateString,backExt);
if (!UploadImage(AsyncUploadFront, frontImage))
{
divMsg.Visible = true;
lblMsg.Text = "Unable to upload cheque front image.";
return;
}
if (!UploadImage(AsyncUploadBack, backImage))
{
divMsg.Visible = true;
lblMsg.Text = "Unable to upload cheque back image.";
return;
}
private bool UploadImage(FileUpload file, string name)
{
string filePath;
string folder = ConfigurationManager.AppSettings["ImagefileLocation"];
filePath = folder + name;
if (File.Exists(filePath))
{
return false;
}
else
{
file.PostedFile.SaveAs(filePath);
return true;
}
}
View to view image
private void loadgrid()
{
imgFront.ImageUrl = "~/ChqImages/" + data.ImageFront;
imgBack.ImageUrl = "~/ChqImages/" + data.ImageBack;
}
<tr>
<td><asp:Label ID="lblImageFront" ForeColor="Black" runat="server" Visible="true" Text ="Image Front"></asp:Label></td>
<td><asp:Image ID="imgFront" runat="server" Width="218px"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblimgBack" ForeColor="Black" runat="server" Visible="true" Text="Image Back"></asp:Label></td>
<td><asp:Image ID="imgBack" runat="server" Width="218px"/></td>
</tr>