I have an application that accepts ID and show product image associated with a company. There are 2 tables, one for oldproducts called ImagesArchive and new product in NewImages. According to requirement an ID can only have one product in the NewImages table. So iam able to successfully do that in my code. But in the ImagesArchive table, since i am recording all the old products from this company there is one to many relationship between ID & product. How can I show a list using iframe & MVC GetOld(ID)?? What's the best way to trap errors and show when Images are not found i.e. URL for product is not working?
--Images Model
public class Images
{
public int ID { get; set; }
public string URL_FilePath { get; set; }
}
HTML file
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function Imagepreview() {
var ID = document.getElementById("ID").value;
document.getElementById("companyImage").src = "api/Images/GetNew/" + ID;
old();
}
function old() {
var ID = document.getElementById("KeyID").value;
document.getElementById("companyOldImage").src = "api/Images/GetOld/" + ID;
}
</script>
<style type="text/css">
#companyImage {
width: 181px;
}
#archiveImage {
margin-left: 17px;
}
</style>
</head>
<body ">
<div class="jumbotron" align="center">
<h1>Images API</h1>
<p class="lead"></p>
<div class="col-md-4" align="center">
<input type="text" name="ID" id="ID"/>
<input type="button" value="Show Image" onclick="Imagepreview()"/>
<br>
<br>
<iframe src="" id="companyImage" height="200" width="200"> </iframe>
<iframe src="" id="companyOldImage" height="200" width="200"> </iframe>
</div>
<div id="response">
</div>
</div>
</body>
</html>
----------------------Controller Get function----------
[System.Web.Mvc.AcceptVerbs("GET")]
public HttpResponseMessage GetNew(int ID)
{
Images newImages = new Images();
GetSettingsInfo();
HttpResponseMessage result = null;
string sql = "select id,url_filepath from [dbo].[NewImages] WHERE ID = @ID";
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand query = new SqlCommand(sql, connection))
{
SqlDataReader rdr;
query.Parameters.Add(new SqlParameter("@ID", ID));
query.CommandType = CommandType.Text;
connection.Open();
query.CommandTimeout = 240;
rdr = query.ExecuteReader();
if (rdr != null)
{
while (rdr.Read())
{
newImages.ID = ConvertFromDBVal<int>(rdr["ID"]);
newImages.URL_FilePath = ConvertFromDBVal<string>(rdr["URL_FilePath"]);
}
}
}
}
if (newImages.KeyID != 0)
{
if (!(newImages.URL_FilePath.Contains("http")))
newImages.URL_FilePath = "http://" + newImages.URL_FilePath;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(newImages.FilePath);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
Image img = Image.FromStream(stream);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(ms.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
}
else
{
result = new HttpResponseMessage(HttpStatusCode.NotFound);
}
return result;
}
[System.Web.Mvc.AcceptVerbs("GET")]
public HttpResponseMessage GetOld(int ID)
{
Images newImages = new Images();
//queries ImagesArchive table instead of ImagesTable
}