1

I have an ASP.NET Core Web API where an endpoint uploads a PDF file. The current method to upload to physical storage (the hard disk) is this:

[HttpPost]
public async Task<ActionResult> Upload([FromForm] UploadModel uploadModel)
{
    var fileName = System.IO.Path.Combine(_environment.ContentRootPath,
                                          "uploads", 
                                          uploadModel.ImeiOrSerial + "" + uploadModel.EquipmentInvoice.FileName);

    await uploadModel.EquipmentInvoice.CopyToAsync(
            new System.IO.FileStream(fileName, System.IO.FileMode.Create));

    using (var context = _context)
    {
         var equipment = new Equipment();

         equipment.TypeOfEquipment = uploadModel.TypeOfEquipment;
         equipment.EquipmentBrand = uploadModel.EquipmentBrand;
         equipment.ModelOrReference = uploadModel.ModelOrReference;
         equipment.ImeiOrSerial = uploadModel.ImeiOrSerial;

         equipment.Path = uploadModel.ImeiOrSerial + "" + uploadModel.EquipmentInvoice.FileName;

         _context.Equipment.Add(equipment);

         await _context.SaveChangesAsync();

         return Ok(await _context.Equipment.FindAsync(equipment.IdEquipment));
     }
}

If I do this in development, everything works perfectly, I get the name of the PDF invoice and it gets in the uploads file inside the solution directory

enter image description here

enter image description here

But if try to do this on my IIS web server, it doesn't work.

I enabled swagger in production mode to see what error the swagger was throwing, but I only get this:

enter image description here

I have no clue what I'm doing wrong.

UPDATE

I wrap my post method in a try catch and now it shows me the following error:

enter image description here

Smuk
  • 35
  • 1
  • 6

2 Answers2

1

Ady's comment is correct. By default, the uploads will not included in publish. You need enable it in your .csproj file. You can check the doc about Visual Studio publish profiles (.pubxml) for ASP.NET Core app deployment.

You can add below code to your .csproj file.

<ItemGroup> 
  <Content Include="uploads\**"> 
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
  </Content> 
</ItemGroup>

enter image description here

enter image description here

If your uploads folder is empty, you can create a simple txt file in it or use MakeDir to create it when publish.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
0

This is what I found that work for me, do the following:

Go to the IIS manager.

Right click on your backend page, in my case its called API:

enter image description here

Then go to Edit Permissions:

enter image description here

Select the option that says IIS_IUSRS, like shown in the previous image, then click in the edit button.

Pick again the option that says IIS_IUSRS and give permission to full control and modify.

Finally go to the wwwroot folder where you published your API and create a new folder where its gonna be located all the files. In my case I named the file "uploads"

enter image description here

And that's it, you should be able to upload files, and locate them in physical storage

Smuk
  • 35
  • 1
  • 6
  • `Finally go to the wwwroot folder where you published your API and create a new folder` ??? – Jason Pan Sep 28 '22 at 01:23
  • It's not the solution for this issue. Next time when you deploy the project(new version), you will know it. – Jason Pan Sep 28 '22 at 01:26
  • Yeah I kwno I will have to create again the same folder each time, besides all the files will desapear, but It was the temporary solution, I still dont know the correct answer. – Smuk Sep 28 '22 at 20:16