locked
Keep getting error 500 while trying to upload file to API RRS feed

  • Question

  • User1750806012 posted

    When i try to get the name from the file it works perfect
    its only shows a error when i try to copy the image to a path:

    HttpPost:

    	[HttpPost]
    		public async Task<ActionResult<Product>> PostProduct([FromForm] ProductFile productFile)
    		{
    
    			if (productFile.File.Length > 0)
    			{
    				string root = _IWebHostEnvironment.ContentRootPath;
    				string fileName = Guid.NewGuid().ToString() + "_" + productFile.File.FileName;
    				string Filepath = Path.Combine(root, "/ClientApp/public/Img/", fileName);
    
    				using (var stream = System.IO.File.Create(Filepath))
    				{
    					await productFile.File.CopyToAsync(stream);
    				}
    				Product newproduct = new Product()
    				{
    					Title = productFile.Title,
    					Description = productFile.Description,
    					Price = productFile.Price,
    					Quantity = productFile.Quantity,
    					FilePath = productFile.File.FileName
    				};
    				_context.products.Add(newproduct);
    				await _context.SaveChangesAsync();
    				return Ok("complete");
    			}
    			else
    				return BadRequest();
    		}

    I can see that the file is uploaded with this code:

    [HttpPost]
    		public async Task<ActionResult<Product>> PostProduct([FromForm] ProductFile productFile)
    		{
    
    			if (productFile.File.Length > 0)
    			{
    return ok()}
    return BadRequest();

    when i turn it arround: 

    if (productFile.File.Length > 0)
    			{
    return BadRequest();
    			}
    return Ok("complete");
    	

    I get a error 400


    So i think with the first code asp net core is able to get the file.
    but for some reason its not allowed to copy that file to my file path?
    would the problem still be on the front-end?

    Wednesday, May 26, 2021 1:34 AM

All replies

  • User1686398519 posted

    Hi osyris, 

    According to the code you provided, this error seems to be caused by a non-existent file path.

    • You can use try-catch statements to capture specific exception information.
      1. If it is an error that the file path does not exist, you can check whether the path exists before using the file path, and create it if it does not exist.
      2. If it is other error message, can you give the specific error message you caught?
    string savepath= Path.Combine(root, "/ClientApp/public/Img/");
    if (!Directory.Exists(savepath))
    {
    Directory.CreateDirectory(savepath); } string Filepath = Path.Combine(savepath, fileName); string errormessage = ""; try {
    using (var stream = System.IO.File.Create(Filepath)) { await productFile.File.CopyToAsync(stream); } }catch(Exception e) { errormessage = e.Message; }

    Best Regards,

    YihuiSun

    Wednesday, May 26, 2021 8:11 AM
  • User1120430333 posted

    The HTTP 500 means an exception was thrown that was unhandled.

    How to Catch All Exceptions in C# & Find All Application Errors – Stackify

    Wednesday, May 26, 2021 8:49 AM
  • User1750806012 posted

    I have found the problem

    string root = Path.Combine(_env.WebRootPath, "/img/");

    have chanced that too:

    string root = _env.WebRootPath + "/img/";

    And then I works perfectly

    apparently your not allowed to use text in the path.Combine?

    Wednesday, May 26, 2021 7:13 PM
  • User1686398519 posted

    Hi osyris, 

    have chanced that too:

    string root = _env.WebRootPath + "/img/";

    string Filepath = Path.Combine(root, "/ClientApp/public/Img/", fileName);

    It looks like your current path is different from the path you provided at the beginning.

    • What you provided at the beginning was "/ClientApp/public/Img/" under the directory containing the content files of the Web service application, and now it is "/img/".

    I works perfectly

    Have you used try-catch statements to capture specific exception information?Did you solve the problem based on the specific error message?

    your not allowed to use text in the path.Combine?

    Do you mean that Path.Combine Method cannot be used?

    • As far as I know, the Path.Combine method is only used to combine strings into path.
    • Can you tell me the specific error message you captured?

    Best Regards,

    YihuiSun

    Thursday, May 27, 2021 3:22 AM
  • User753101303 posted

    Hi,

    Weird. Seems the differeicen is rather that you changed ContentRootPath to WebRootPath.

    This is not the problem but how you fixed it. 500 means that you have a server side error which by default should be wrriiten to the Windows event log. Once you have ythe error message - really knowing which probme you have - it is then usually mcuh easier to solve rhan when trying to guess from the code (more lkkely you were trying to write to an non existing folder because of the wrong root locartion).

    Friday, May 28, 2021 8:23 AM