how to upload image in url path in asp net core web api
Loading
how to upload image in url path in asp net core web api
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Jul 2, 2023, 10:28 AM
To improve the code with test cases, you can use a testing framework like NUnit or xUnit to write unit tests for the UploadImage method.
The first test case, UploadImage_ValidImage_ReturnsOkResult, checks if the UploadImage method returns an OkObjectResult when a valid image is uploaded. The second test case, UploadImage_NoFile_ReturnsBadRequest, verifies that the method returns a BadRequestObjectResult when no file is uploaded. You can add more test cases to cover different scenarios, such as when the file size exceeds the limit or when an invalid file type is uploaded. Use assertions to verify the expected behavior of the UploadImage method.
Vishal YelvePosted Jun 28, 2023, 1:15 PM
Try this
Reference My article: https://www.c-sharpcorner.com/article/net-core-3-1-web-api-crud/
Deepak RawatPosted Jun 28, 2023, 8:29 AM
Define a model for the image upload request, which includes a property to hold the image data.
create an action method that accepts the image upload request and handles the file upload. Use the
HttpPostattribute to indicate that the action method accepts HTTP POST requeststhe image is saved to the
wwwroot/imagesfolder using a unique file name generated byGuid.NewGuid(). You can modify the file path and naming strategy as per your requirement.Configure the static files middleware to serve the uploaded images. In the
Configuremethod of yourStartupclassadjust the file path and URL path based on your application's requirements. Also, ensure that the appropriate file system permissions are set to allow writing and reading the image files.
Now, when you send a POST request to the
/uploadendpoint with the image file in the request body, it will be saved to the specified URL path, and the URL path will be returned in the response.Amit MohantyPosted Jun 28, 2023, 8:13 AM
For more details check the below links.
https://www.c-sharpcorner.com/blogs/upload-image-in-asp-net-core-web-api-60-with-postman
https://stackoverflow.com/questions/73498624/how-do-i-upload-an-image-in-asp-net-core-web-api-with-other-form-data
https://www.codingsonata.com/file-upload-with-data-using-asp-net-core-web-api/