Search code examples
c#multipartform-datafilestream

StreamContent doesn't take the length of the stream


I am interacting with dropbox and I get a file by it's path, read it out as a stream, copy the content of that stream into a filestream and use that filestream to make a StreamContent object which I can append to my multipart/form-data:

[FunctionName("SendFiles")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req,
            ILogger log)
{
    ...
    DropboxClient dropbox = new DropboxClient("token");

    MultipartFormDataContent formdata = new MultipartFormDataContent();
    string name = GetFileName(filePath);
    string tempfile = Path.GetTempFileName();
    string tempFilePath = getTempFilePath(tempfile, name);
    FileStream fs = File.Create(tempFilePath);
    using (var res = await dropbox.Files.DownloadAsync(filePath))
    {
          (await res.GetContentAsStreamAsync()).CopyTo(fs);
    }

    HttpContent content = new StreamContent(fs);

    content.Headers.Add("Content-Type", GetFileType(name));
    formdata.Add(content, "files", name);
    // System.IO.File.Delete(tempfile); at this point, we still are using the file for some reason and so I can't delete it
    // rest of the method, sending the content using httpClient
...
}

private static string getTempFilePath(string tempfile, string name)
{
    char[] charSeparators = new char[] { '\\' };
    var splitPath = tempfile.Split(charSeparators);
    splitPath[splitPath.Length - 1] = name;
    return String.Join("\\", splitPath);
}

private static string GetFileName(string path)
{
    char[] charSeparators = new char[] { '/' };
    var splitPath = path.Split(charSeparators);
    return splitPath[splitPath.Length - 1];
}

private static string GetFileType(string name)
{
    char[] charSeparators = new char[] { '.' };
    var splitName = name.Split(charSeparators);
    string extension = splitName[1].ToLower();
    switch (extension)
    {
        case "pdf":
              return "application/pdf";
        case "png":
              return "image/png";
        case "doc":
              return "application/msword";
        case "docx":
              return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        case "txt":
             return "text/plain";
        case "tif":
             return "image/tif";
        case "jpg":
            return "image/jpg";
        case "rtf":
            return "application/rtf";
        // default == not supported type, we don't set content type
        default:
            return "";
     }
}

When I'm debugging, I can clearly see that the length of the stream gets updated when copying to the filestream but when I then use the filestream to create the stream content, the content's length remains 0. How is this happening and what can I do to fix this?


Solution

  • You should be able to feed the stream from the dropbox client directly into the StreamContent like this:

    HttpContent content = new StreamContent(await res.GetContentAsStreamAsync());