【问题标题】:Upload file multipartformdata to Hubspot api将文件 multipartformdata 上传到 Hubspot api
【发布时间】:2020-02-25 11:52:05
【问题描述】:

我正在尝试将文件上传到 hubspot api。 https://developers.hubspot.com/docs/methods/files/post_files 当我查看 CRM 时,一切看起来都不错,只是有些文件名看起来很奇怪。

文件已创建并获得文件大小,但接缝已损坏,因为我无法打开或下载它们。

我的方法是这样的

   private async Task<HsCreateFileResponse> UploadAttachment(string pStream, string pFilename)
    {
        string url = API_BASE_URL + "filemanager/api/v2/files?hapikey=" + API_KEY;
        string path = @"C:\Temp filer\";
        HttpClient client = new HttpClient();
        var content = new MultipartFormDataContent();
        content.Headers.Add("hidden", "true");

        using (var s = new MemoryStream())
        {
            using (var writer = new StreamWriter(s))
            {
                writer.Write(pStream);
                s.Position = 0;
                using (var fs = new FileStream(path + pFilename, FileMode.Create))
                {
                    s.CopyTo(fs);
                }
                writer.Flush();
            }
        }
        using (var fs = new FileStream(path + pFilename, FileMode.Open, FileAccess.Read))
        {
            byte[] bytes = new byte[fs.Length];
            int numBytesToRead = (int)fs.Length;
            int numBytesRead = 0;

            while (numBytesToRead > 0)
            {
                int n = fs.Read(bytes, numBytesRead, numBytesToRead);
                if (n == 0)
                {
                    break;
                }
                numBytesRead += n;
                numBytesToRead -= n;
            }

            numBytesToRead = bytes.Length;
            System.Threading.Thread.Sleep(100);
            content.Add(new StreamContent(new MemoryStream(bytes)), "files", pFilename);
            var res = await client.PostAsync(url, content);
            return JsonConvert.DeserializeObject<HsCreateFileResponse>(await res.Content.ReadAsStringAsync());
        }

    }

【问题讨论】:

    标签: c# rest hubspot


    【解决方案1】:

    将其添加到内容中:

    content.Add(new StringContent("my-folder/and-subfolder"), "folder_paths");
    content.Add(new StringContent("my-file-name.png"), "file_names");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多