“no_file_data” error when trying to upload file to slack

前端 未结 2 424
暗喜
暗喜 2021-01-20 07:40

when I try to upload any kidn of file through my SlackApp(via c# using HttpClient), I allways get the following response:

{\"ok\":false,\"error\":\"no_file_d         


        
2条回答
  •  伪装坚强ぢ
    2021-01-20 08:38

    You have two bugs in your code:

    • main(): The parameter to specify the channels is called channels, not channel
    • UploadFile(): When you add your file content to the multipart you need to include the correct API parameter for the file which is file, not slack. And also want to include a reasonable filename (instead of slack.txt).

    Additional comments

    • UploadFile(): Its wrong to set the content type to multipart/form-data. The correct type for that content would be image/jpeg. However, the correct type seams to be detected automatically, so just remove the line.
    • main(): The Slack API will always return OK (http 200, unless there is a network problem), so you want to also look on the ok and error properties of the JSON response instead.

    Here is an update version of your code. I changed your main() method to include a call to `UploadFile()?.

    using System;
    using System.IO;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    
    namespace SlackApp
    {
        public class PostFile
        {
            string path = @"C:\Users\Stratios_down.jpg";
    
            public byte[] ReadImageFile()
            {
                FileInfo fileInfo = new FileInfo(path);
                long imageFileLength = fileInfo.Length;
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
                BinaryReader br = new BinaryReader(fs);
                byte[] imageData = br.ReadBytes((int)imageFileLength);
                return imageData;
            }
        }
    
        public class SlackClient
        {
            private readonly Uri _webhookUrl;
            private readonly HttpClient _httpClient = new HttpClient { };
    
            public SlackClient(Uri webhookUrl)
            {
                _webhookUrl = webhookUrl;
            }
    
            public async Task UploadFile(byte[] file)
            {
                var requestContent = new MultipartFormDataContent();
                var fileContent = new ByteArrayContent(file);            
                requestContent.Add(fileContent, "file", "stratios.jpg");
    
                var response = await _httpClient.PostAsync(_webhookUrl, requestContent);
                return response;
            }
        }
    
        class TestArea
        {
            public static void Main(string[] args)
            {
                Task.WaitAll(IntegrateWithSlackAsync());
            }
    
            private static async Task IntegrateWithSlackAsync()
            {
                var webhookUrl = new Uri(
                    "https://slack.com/api/files.upload?token=xoxp-MY-TOKEN&channels=test"
                );
                var slackClient = new SlackClient(webhookUrl);
    
                PostFile PF = new PostFile();
                var testFile = PF.ReadImageFile();
    
                var response = await slackClient.UploadFile(testFile);
    
                string content = await response.Content.ReadAsStringAsync();
                Console.WriteLine(content);
                Console.ReadKey();
    
            }
        }
    }
    

    In addition I would have a couple of suggestions to improve your code.

    • Instead of including the additional API parameters in the URL, I would send them in the POST request as recommended by the API documentation.
    • Including the file as FileStream instead of loading it yourself into a ByteArray is the better approach and recommended for larger files.
    • Not sure why you need an infinite loop in your main. Those are really bad and should be avoided.

    Please also take also a look at my new async example for uploading a file to Slack where I applied those two ideas.

提交回复
热议问题