I'm not an ASP.NET MVC expert but I need to handle a small project and need some help.
Basically what I'm doing is an MVC project for submitting or listing some text reports and I used database first approach on the model. So I have this model of the tables I've created before, without doing a good plan. After starting the project, I happened to be obliged to add some images to it which wasn't a case while I was designing the data model and I solved this issue in a very poor way, via going out of the MVC philosophy but it worked.
So I added an "img" folder to the project and put the .jpeg files inside.
..and I showed the images in view like that among the data coming from the model:
...@foreach (var item in Model) {
<tr>
<td>
<img src="~/img/@string.Format("{0}.jpeg",item.emailid)"/>
</td>
<td>
@Html.DisplayFor(modelItem => item.emailid)
</td>
<td>
@Html.DisplayFor(modelItem => item.date)
</td>
<td>
@Html.DisplayFor(modelItem => item.task1)
</td>.......
And then I exported that view to the excel file using this code:
public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource = db.web_weeklyreports.Include(w => w.web_kategori).Include(w => w.web_kategori1).Include(w => w.web_kategori2).Include(w => w.web_kategori3).Include(w => w.web_kategori4).ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=WeeklyReports.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("adminreports");
}
This code worked well except the image because on DataSource there is only model information and my images are not located in Model, they are in a folder inside the solution as shown above.
So I'm able to export my view except for images and I need to get those images to excel report too.
So this is my view with images
Any help?
I solved it with this class
Now I can export images like this: