0

I am working on an angular and .NET Core application. I have to pass the file uploaded from angular to WEB API. My code is:

public async Task ImportDataScienceAnalytics(string authToken, IFormFile file)
{
        var baseUrl = Import.GetBaseURL();

        var client = new RestClientExtended(baseUrl + "algorithm/import");
        var request = new RestRequest(Method.POST);


        request.AddHeader("authorization", authToken);
        string jsonBody = JsonConvert.SerializeObject(file);
        request.AddJsonBody(jsonBody);

        var response = await client.ExecutePostTaskAsync(request);
        var result = response.Content;
}

Issue is that i get "No Attachment Found". I think the issue is because of IFormFile. How can i resolve this issue so that i can upload the file to web api.

Waleed Naveed
  • 2,293
  • 2
  • 29
  • 57
  • 1
    Why are you using `async void`? I'd recommend using `Task` instead. – haldo Jun 26 '20 at 14:22
  • check that file is populated.... also you would need to read from the stream IFormFile is and interface for interacting with the file stream. further/also why are you `JsonConvert.SerializeObject(file);` , its like you trying to upload to pass on. – Seabizkit Jun 26 '20 at 14:24
  • I have to pass this file to an API @Seabizkit . . .How can i read the stream from IFormFile and pass it to API, any example code ? – Waleed Naveed Jun 26 '20 at 14:25
  • 1
    First of all put a breakpoint and check if IFormFile file has content. Secondly you need create stream. `if (file.Length > 0) { var filePath = Path.GetTempFileName(); using (var stream = System.IO.File.Create(filePath)) { await file.CopyToAsync(stream); } }` – bayram Jun 26 '20 at 14:27
  • @WaleedNaveed bayram has giving you the answer above ;-) – Seabizkit Jun 26 '20 at 14:29
  • Yes, IFromFile does have content. @bayram how will i pass the file to web api, i have to add it to the body of request . My file is a zip file – Waleed Naveed Jun 26 '20 at 14:36
  • From where do you want to pass file to web api? I do not get the idea. From your C# code u want to call external web api and upload it there or what? If so take a look at https://stackoverflow.com/questions/42633554/client-code-to-upload-file-via-asp-net-mvc-webapi – bayram Jun 26 '20 at 15:25
  • yes from my C# code, i want to pass it to an external API @bayram – Waleed Naveed Jun 26 '20 at 15:30
  • Fix the `async void` first. That's messing up the lifetime of the IFormFile. – Tratcher Jun 26 '20 at 18:54
  • I did that in my code but nothing happened. I have even updated the question now @Tratcher – Waleed Naveed Jun 26 '20 at 20:59

2 Answers2

0

It seems that you'd like to post uploaded file to an external API from your API action using RestClient, you can refer to the following code snippet.

var client = new RestClient(baseUrl + "algorithm/import");
var request = new RestRequest(Method.POST);

request.AddHeader("authorization", authToken);


using (var ms = new MemoryStream())
{
    file.CopyTo(ms);
    var fileBytes = ms.ToArray();

    request.AddFile("file", fileBytes, file.FileName, "application/octet-stream");
}

//...

Testing code of Import action

public IActionResult Import(IFormFile file)
{
    //...

    //code logic here
Fei Han
  • 26,415
  • 1
  • 30
  • 41
0

You need to make following changes to the code. var baseUrl = Import.GetBaseURL();

        var client = new RestClientExtended(baseUrl + "algorithm/import");
        var request = new RestRequest(Method.POST);

        byte[] data;
        using (var br = new BinaryReader(file.OpenReadStream()))
            data = br.ReadBytes((int)file.OpenReadStream().Length);
        ByteArrayContent bytes = new ByteArrayContent(data);
        MultipartFormDataContent multiContent = new MultipartFormDataContent
    {
        { bytes, "file", file.FileName }
    };
        //request.AddHeader("authorization", authToken);
        //string jsonBody = JsonConvert.SerializeObject(file);
        //request.AddJsonBody(jsonBody);

        /// Pass the multiContent into below post
        var response = await client.ExecutePostTaskAsync(request);
        var result = response.Content;

Do not forget to pass the variable multiContent into the post call.

Shivendra
  • 159
  • 1
  • 7