C
C#4w ago
Alex

Upload file with swagger ui crash

I'm trying to upload IFormFIle with swagger ui and as soon as I select file and click select or cancel the backend application crashes without any error displayed. Can I fix it? Is it better to switch to Postman?
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile? formFile)
{
if (formFile == null || formFile.Length == 0)
{
return BadRequest("No file uploaded.");
}

var putObjectArgs = new PutObjectArgs()
.WithBucket(_config.BucketName)
.WithObject(formFile.FileName)
.WithObjectSize(formFile.Length)
.WithContentType(formFile.ContentType)
.WithStreamData(formFile.OpenReadStream());

// TODO: check if object is uploaded to the bucket
await _minio.PutObjectAsync(putObjectArgs);

return Ok(new {message = "File uploaded to the bucket"});
}
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile? formFile)
{
if (formFile == null || formFile.Length == 0)
{
return BadRequest("No file uploaded.");
}

var putObjectArgs = new PutObjectArgs()
.WithBucket(_config.BucketName)
.WithObject(formFile.FileName)
.WithObjectSize(formFile.Length)
.WithContentType(formFile.ContentType)
.WithStreamData(formFile.OpenReadStream());

// TODO: check if object is uploaded to the bucket
await _minio.PutObjectAsync(putObjectArgs);

return Ok(new {message = "File uploaded to the bucket"});
}
No description
No description
8 Replies
Angius
Angius4w ago
Run the debugger, step through the code, see where exactly it crashes?
kurumi
kurumi4w ago
did you try debug your code? I Guess the problem is how you uploading file means it is after 1st if
Alex
AlexOP4w ago
The application crashes right after I click "open" or "cancel" button on the file selection window. The endpoint works fine if I send file with Postman. I'm curious if anybody encountered this problem in swagger ui before and knows how to solve it
kurumi
kurumi4w ago
then have no idea
FLOki
FLOki4w ago
I'm alwo writing a file upload endpoint. No issues uploading files so far. Feel free to glance at my code and follow the code-review here: https://discord.com/channels/143867839282020352/1300831004411428994 What happens if you cut out putObjectArgs entirely?
C#
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile? formFile)
{
if (formFile == null || formFile.Length == 0)
{
return BadRequest("No file uploaded.");
}

return Ok(new { message = "File uploaded to the bucket" });
}
C#
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile? formFile)
{
if (formFile == null || formFile.Length == 0)
{
return BadRequest("No file uploaded.");
}

return Ok(new { message = "File uploaded to the bucket" });
}
Also, why is formFile nullable?
Alex
AlexOP4w ago
Do you upload with swagger? The endpoint is totally fine and works if I upload file with the postman. The problem is that it doesn't even hit this endpoint (I guess). Nullable if because the file might not be sent with the request
FLOki
FLOki4w ago
Yes. Both Swagger and Postman work when I upload a file Put a debugger in your Program.cs file where you're mapping the contollers and step through. If everything else fails generate a fresh project and move your endpoint there to see what the problem is. What also helps is posting a ling to your repo to see your project setup and configuration
Alex
AlexOP4w ago
I found the problem and it actually was a browser Brave. I don't know why exactly it crashes my app, but I switched to other and it's working fine now
Want results from more Discord servers?
Add your server