-2

I wish the result to be a success. But what is produced does not enter into the if and else blocks

public async Task<UploadServicesResult> HandleFileUpload(FileInfo file, string path)
{
    var fileupload = new StreamContent(new FileStream(file.FullName, FileMode.Open));
    multipartContent.Add(fileupload, "file", file.Name);
    multipartContent.Add(new StringContent(path), "folder");
    multipartContent.Add(new StringContent(s3key), "api_key");

    using (HttpResponseMessage response = await httpClient.PostAsync(server, multipartContent))
    {
        if (response.IsSuccessStatusCode)
        {
            string responseBody = await response.Content.ReadAsStringAsync();
            var messageObj = JsonConvert.DeserializeObject<JObject>(responseBody);
            string url = messageObj.Value<string>("url");

            return new UploadServicesResult { IsSuccess = true, Message = url };
        }
        else
        {
            return new UploadServicesResult { IsSuccess = false, Message = "Upload failed" };
        }
    }
}
Julian
  • 312
  • 2
  • 14
  • 1
    Have you tried to manually make a `POST` request to your `server` with postman? Do you get an response there? It seems that the problem is in your api. – Julian Aug 28 '23 at 09:12
  • I've tried it on postman and it works. But it doesn't work in my c# project – Dhur Rohma Aug 28 '23 at 09:35
  • Can you please provide more information in your question? For example how you make the request in postman and what you get as an result, next, it would be helpful to know where the `multipartContent` and `server` comes from. There is very likley something wrong with the `multipartContent` part. – Julian Aug 28 '23 at 09:48
  • in postman i use form-data with key file for upload file, folder is string of folder path, api_key is string of api key. Result : { "success": true, "message": { "url": (url to access file), "folder": (the folder where the file is stored), "filename": "uploads" } } – Dhur Rohma Aug 28 '23 at 10:03
  • you may want to take a look at this: https://stackoverflow.com/a/10744043/16871250 – Julian Aug 28 '23 at 10:16

0 Answers0