【问题标题】:How to build base64 rest web service to upload image file with other information如何构建base64 REST Web服务以上传带有其他信息的图像文件
【发布时间】:2016-01-27 00:50:55
【问题描述】:

我想构建一个 Rest Web 服务(在 .NET 中)来上传带有名称、描述、时间等其他信息的图像文件。

所以我写了这段代码:

[Route("SaveDocument")]
[HttpPost]
public HttpResponseMessage SaveDocument(Stream fileContents)
{

    byte[] buffer = new byte[10000];
    int bytesRead, totalBytesRead = 0;
    do
    {
    bytesRead = fileContents.Read(buffer, 0, buffer.Length);
    totalBytesRead += bytesRead;
    } while (bytesRead > 0);
    Console.WriteLine("Service: Received file {0} with {1} bytes", fileName, totalBytesRead);

}

我想要传递文件 Base64,但我无法做到这一点。

我们能帮帮我吗?

【问题讨论】:

标签: c# .net web-services file-upload


【解决方案1】:

您应该使用您的附加信息将您的 base64 数据包装在一个 JSON 对象中,并在您的 REST 后端解析它:

{
    "fileName": "example.jpg",
    "content": "base64-content",
    "creationTime": "2015-10-27T00:10:00"
}

在服务器端为您的文件创建一些模型,并将您的响应解析为具有以下内容的对象:https://msdn.microsoft.com/en-us/library/hh674188.aspx

然后您可以使用以下命令将文件保存到后端的磁盘:

File.WriteAllBytes(@"c:\yourfile", Convert.FromBase64String(yourBase64String));

【讨论】:

    猜你喜欢
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多