Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am new to WebAPI in My form submits to the server successfully, and then my server's WebAPI has to change my img's src attribute. I can not figure out how to do this. Do I need to use HttpContext.Current? or maybe HtmlHelper? or should I be using <frombody()>? Is there a better way? Below is the skeleton code. I am using Visual Studio 2019 on 4.8 .net framework and

<form action="/api/Values " method="post" enctype="multipart/form-data">
          <input type="file" id="btn_FileUpload" name="btn_FileUpload">
          <input type="submit" id="btn_submit" name="btn_submit">
	      <img id="img1" name="img1" src="@Url.Content("/IntroImage.png")" />

Public Class ValuesController
    Inherits ApiController

    Public Sub UploadFile1()
	     Should I use HttpContext.Current ?
    End Sub

    Public Sub UploadFile2(<FromBody()> ByVal value As String)
         Should I useFromBody ?
    End Sub
End Class

What I have tried:


Dim l_obj As Object = HttpContext.Current.Session.Item("img1")
Updated 23-Aug-21 21:34pm

1 solution

Unless your API is returning a complete HTML page - which isn't normal for an API - it cannot change the source of an HTML element in your page.

Assuming you're using an AJAX/fetch request to submit the form to the API, your Javascript can then change the image source when the request completes.

Fetch API - Web APIs | MDN[^]
Share this answer
DougSchofield 24-Aug-21 10:40am    
Thanks. I'll have to learn AJAX.
DougSchofield 26-Aug-21 19:42pm    
Your advice was correct. I learned and implemented an AJAX solution and it did what I needed. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900