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.

10
  • 1
    Why are you using async void? I'd recommend using Task instead. Commented Jun 26, 2020 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. Commented Jun 26, 2020 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 ? Commented Jun 26, 2020 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); } } Commented Jun 26, 2020 at 14:27
  • @WaleedNaveed bayram has giving you the answer above ;-) Commented Jun 26, 2020 at 14:29

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.