0

Can anyone please suggest a swift code example to send a file to the below asp.net core method? Actually, file is being uploaded correctly from the postman, swagger but it is not working via swift. Thanks in advance...

[HttpPost("AddAttachment")]
    public async Task<IActionResult> AddAttachment([FromForm] IFormFile file)
    {
      
        return await _handler.AddAttachment(file);
    }
Cipher
  • 29
  • 1
  • 5
  • This action simply expects a FORM POST with a single `file` input. What have you tried? What was the problem? Post your code and actual error messages. This is a Swift question without any Swift code – Panagiotis Kanavos May 12 '22 at 06:49
  • There are several duplicates too, like [Upload image with multipart form-data iOS in Swift](https://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift). The strange thing in this question though is that `Email` is read from a request header instead of a form field. Why? – Panagiotis Kanavos May 12 '22 at 06:51
  • Also, the nested `Task.Run` and `await` calls only waste threads. In web apps, every request is served by a different Threadpool thread. There's no reason to spawn yet another thread to do what the current thread could easily do. This only wastes time switching from one thread to another. `async/await` is used in IO operations to allow the threadpool thread to serve another request until the IO operation completes. – Panagiotis Kanavos May 12 '22 at 06:55
  • Please ignore the async await or email. We just want a swift code that can send file which can be received in IFormFile of Asp.net core Api method – Cipher May 12 '22 at 07:00
  • I already answered that. `IFormFile` is the type used to receive `input type="file"` values. You need to make a FORM POST with a file. There are many answers that show how to do this. You haven't posted any code though, and the C# code has serious problems. A simple `FORM POST` would work if that method didn't try to read the header. The way it is now, the method could throw if the header is missing. Forms don't set headers so submitting a form to that action would result in errors – Panagiotis Kanavos May 12 '22 at 07:08
  • Did you check [Upload image with multipart form-data iOS in Swift.](https://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift)? Have you tried the code? Did you get any errors and if so what were they exactly? – Panagiotis Kanavos May 12 '22 at 07:12
  • @PanagiotisKanavos We have tried that code earlier as well that resulted in below error: Failed to read the request form. Unexpected end of Stream, the content may have already been read by another component. – Cipher May 12 '22 at 07:27

0 Answers0