【发布时间】:2014-02-16 04:34:34
【问题描述】:
我有一个 Web 服务,它获取画布标记的内容并将其保存到 MongoDB GridFS 存储中。
下面的代码可以工作,但是它需要在将图像发送到 MongoDB 之前将其保存到磁盘。
Using postBody As Stream = Request.InputStream
' Get the body of the HTTP POST (the data:image/png)
postBody.Seek(0, SeekOrigin.Begin)
Dim imageData As String = New StreamReader(postBody).ReadToEnd
Dim base64Data = Regex.Match(imageData, "data:image/(?<type>.+?),(?<data>.+)").Groups("data").Value
Dim data As Byte() = Convert.FromBase64String(base64Data)
Using stream = New MemoryStream(data, 0, data.Length)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
Dim directory = Server.MapPath("~/App_Data/temp/")
Dim file = String.Concat(directory, id, ".png")
img.Save(file, System.Drawing.Imaging.ImageFormat.Png)
Using fs = New FileStream(file, FileMode.Open)
db.GridFS.Upload(fs, id & ".png")
End Using
End Using
End Using
有没有更好的方法,可能不需要在上传到 MongoDB 之前将其持久化到磁盘?
【问题讨论】:
-
为什么不直接将内存流传递给它,将
Position设置回0?
标签: vb.net mongodb base64 gridfs