c# - 如何使用 C# 将 base64 值从数据库转换为流

标签 c# stream

我有一些 base64 存储在数据库中(实际上是图像)需要上传到第三方。我想使用内存上传它们,而不是将它们保存为图像然后将其发布到服务器。这里有人知道如何将 base64 转换为流吗?

如何更改此代码:

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

改为使用图像文件的 base64 解释填充 StreamContent 对象。

    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 = "\"name\"",
        };
        fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

        return fileContent;
    }

最佳答案

从数据库中获取字符串后,您将需要执行类似的操作:

var bytes = Convert.FromBase64String(base64encodedstring);
var contents = new StreamContent(new MemoryStream(bytes));
// Whatever else needs to be done here.

关于c# - 如何使用 C# 将 base64 值从数据库转换为流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524343/

相关文章:

c# - c#方法调用上的'@'符号前缀?

c# - 存储库、工厂和层次结构数据

javascript - 如何在 gulp 构建期间将内容插入文件?

node.js - 如何在 Node.js 中使用 fs.createReadStream 按单词(以空格分隔并返回)读取文件,例如 `/\s+/g`?

c# - 为什么这个副本 Stream 比它原来的 Stream 大?

c# - 二维对象数组返回类型 - NSubstitute

c# - 如何对文本执行逐行和逐字符差异?

html - 如何在html5中绘制音频流的波形?

c# - 数据库代码优先 Entity Framework

Java - 捕获 System.err.println 或捕获 PrintStream