Looks like there are a few issues in your code:
- Image not displaying
The following is a local path on the server, which won't work in the browser.
src='C:/Users/user/source/repos/Myapiwithmvc7_6_25/Contain/Image/" + item.path
You need to serve the uploaded images as static files from the API and reference them using a public URL.
a) Serve Static Files from the API:
In your API project:
Startup.cs / Program.cs
app.UseStaticFiles(); // Make sure this is added
Inside wwwroot Folder (recommended)
Move your images to wwwroot/images
.
Modify your upload path:
string uploadfolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/");
Update returned path:
return "/images/" + uniquefilename; // relative URL to be used in img src
b) Correct image path in view:
Update your JavaScript:
"<td data-label='Image'><img class='rsponsimage' src='" + item.path + "' alt='' /></td>"
If you return just file name (item.path = "image.jpg"
), then:
src='https://yourapiurl/images/" + item.path + "'
- Inserting image from MVC view
Your API is expecting multipart/form-data
for image upload, but you're sending JSON.
In your MVC controller:
a) Add Form with File Upload:
<form enctype="multipart/form-data" asp-controller="Product" asp-action="InsertRecord" method="post">
<input type="text" name="productname" />
<input type="file" name="imagepath" />
<button type="submit">Submit</button>
</form>
b) Modify MVC Controller to Send Multipart Form Data:
[HttpPost]
public async Task<IActionResult> InsertRecord(tbl_product model)
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StringContent(model.productname), "productname");
if (model.imagepath != null && model.imagepath.Length > 0)
{
var streamContent = new StreamContent(model.imagepath.OpenReadStream());
content.Add(streamContent, "imagepath", model.imagepath.FileName);
}
HttpResponseMessage response = await _Client.PostAsync(_Client.BaseAddress + "/Product/Inserprodcut", content);
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
return View(model);
}
- Updating record with a new image
Similar to insert, your update should also send multipart/form-data.
MVC Update Controller:
[HttpPost]
public async Task<IActionResult> Updatedata(tbl_product model)
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StringContent(model.productID.ToString()), "productID");
content.Add(new StringContent(model.productname), "productname");
if (model.imagepath != null)
{
var streamContent = new StreamContent(model.imagepath.OpenReadStream());
content.Add(streamContent, "imagepath", model.imagepath.FileName);
}
HttpResponseMessage response = await _Client.PutAsync(_Client.BaseAddress + "/Product/Productupdate", content);
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
return View(model);
}
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin