Open Closed

An custom Web API controller not working after migrated to 7.4 #6005


User avatar
0
scottie.tang@cpy.com.hk created
  • ABP Framework version: v3.1.0
  • UI Type: MVC
  • Database System: MongoDB
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

Hi Support,

We have recently completed the migration of our application from ABP 3.1.0 to 7.4. During the process, we encountered an issue with our old UploadController, which is an additional Web API controller responsible for handling file uploads via a simple POST request. While this code worked fine in version 3.1.0, it seems to be causing problems in the upgraded version.

I would greatly appreciate your advice or guidance on this matter. Is there any specific configuration that needs to be done in the WebModule or any other steps we should take to ensure the proper functioning of the UploadController in ABP 7.4?

Thank you for your assistance.

Below is our code snippet. ( We put the code in Controllers in HttpApi )

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace TEST.Controllers
{
    public class UploadController : AbpController
    {
        private readonly IWebHostEnvironment _hostingEnvironment;
        public UploadController(IWebHostEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        [HttpPost("UploadFile")]
        public async Task<JsonResult> UploadFile([FromForm] IFormFile file)
        {
            try
            {
                var webRootPath = _hostingEnvironment.WebRootPath + "\\Uploads\\";
                var folderPath = Guid.NewGuid() + "/";
                var fullFolderPath = webRootPath + folderPath;
                System.IO.Directory.CreateDirectory(fullFolderPath);
                var fullFilePath = fullFolderPath + file.FileName;
                using (var stream = new FileStream(fullFilePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                return new JsonResult(new
                {
                    Success = true,
                    FileName = "/Uploads/" + folderPath + file.FileName
                });
            }
            catch
            {
                return new JsonResult(new
                {
                    Success = false
                });
            }
        }
    }
}

1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can check the code: https://github.com/abpframework/abp/blob/dev/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentTestController.cs#L46

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11