0

I'd like to be able to change this code so that I don't have to pull from a file on a file system, but rather use a base64 value that is saved in a database. Does anyone here know enough about StreamContent to know what I need to do to accomplish this?

The file is a jpeg file.

    private static StreamContent FileMultiPartBody(string fullFilePath)
    {

        var fileInfo = new FileInfo(fullFilePath);

        var fileContent = new StreamContent(fileInfo.OpenRead());

        // Manually wrap the string values in escaped quotes.
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            FileName = string.Format("\"{0}\"", fileInfo.Name),
            Name = "\"signature\"",
        };
        fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

        return fileContent;
    }
J Hunt
  • 733
  • 1
  • 10
  • 21
  • Looks like `StreamContent` constructor accepts `Stream`. So you'll just need to create a stream from your string. Maybe http://stackoverflow.com/questions/1879395/how-to-generate-a-stream-from-a-string can help? – Steven V Jul 20 '15 at 16:39

1 Answers1

0

StreamContent is just a wrapper of another stream (the stream returned from fileInfo.OpenRead() in your example). All you need to do is replace that stream with a stream from your database and return that. You can also replace the fileInfo.Name with a Path.GetFileName(fullFilePath) call.

private Stream GetStreamFromDatabase(string fullFilePath)
{
    //TODO
}

private static StreamContent FileMultiPartBody(string fullFilePath)
{

    var fileContent = new StreamContent(GetStreamFromDatabase(fullFilePath))

    // Manually wrap the string values in escaped quotes.
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
        FileName = string.Format("\"{0}\"", Path.GetFileName(fullFilePath)),
        Name = "\"signature\"",
    };
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

    return fileContent;
}

If you need help converting a base64 value from a database to a stream I would suggest asking a separate question about that.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431