What is good practice for sending image files to the IIS web server?

Dean Marinov 20 Reputation points
2024-08-04T10:11:43.16+00:00

Hello,

I wrote an Office AddIn that photographs an Excel sheet range and transfers the image to an ASP.NET WebAPI on our internal IIS server. I also added MVC functionality to the API to allow the image to be displayed in a View.

But I'm unsure whether a WebAPI is a good choice for this. Everything works as it should and the purpose of the whole development is to set up the image as a 'standard website' for the employees - i.e. the page is set as default via GPO.

How would you do that, or what would be good practice here? I think a WebAPI does involve more security effort, and since it doesn't even support views by default, I'm not sure whether this would be a good solution for this simple case.

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
333 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,251 Reputation points Microsoft Vendor
    2024-08-05T02:03:07.39+00:00

    Hi @Dean Marinov,

    You can upload images from a server folder using ASP.NET MVC.

    You can follow this article to upload image in MVC5 application. Upload Images on Server Folder Using ASP.NET MVC

    Refer to the following code:

    [HttpPost]
            public ActionResult Upload(HttpPostedFileBase file)
            {
                Uri requestUri = HttpContext.Request.Url;
                string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
                if (ModelState.IsValid)
                {
                    try
                    {
                        if (file != null)
                        {
                            string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName));
                            file.SaveAs(path);
                        }
                        string link = baseUrl + "/Images/" + file.FileName;
                        ViewBag.FileStatus = "File uploaded successfully.";
                        //Return the url of image
                        ViewBag.FileLink = link;
                    }
                    catch (Exception e)
                    {
                        ViewBag.FileStatus = "Error while file uploading. Error message is "+e.Message;
    
                    }
                }
                return View("Index");
            }
    

    Index.cshtml add this to show url.

    <div class="col-md-offset-2 col-md-10 text-success">
            @ViewBag.FileLink
    </div>
    

    Don't forget to add a folder named Images to store images. After publishing, you also need to add it in published folder, otherwise error message will show that cannot find folder.

    Published folder

    It works well.

    enter image description here

    enter image description here

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.