[英]How to build base64 rest web service to upload image file with other information
我想构建一个 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,但我无法做到这一点。
我们能帮帮我吗?
您应该使用附加信息将 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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.