如何将其转换为图像以存储在 SQL 中
尝试上传带有文件输入的表单,仅从输入流获取表单字段,而不是 File.Request 方法。
该表单包含一个文件输入和几个文本框,因此我不能将流上传到数据库。
我使用此方法转换为字符串
int len = Convert.ToInt32(context.Request.InputStream.Length);
byte[] stream = new byte[len];
context.Request.InputStream.Read(stream, 0, len);
string mime = Encoding.UTF8.GetString(stream);
,然后在边界处分割多部分/表单数据,并读取每个部分的第一行以查看它是否是文件。您可以在此处查看完整代码
文件将如下所示
-----------------------------17901701330412
Content-Disposition: form-data; name="file"; filename="IMG00004-20101209-1704.jpg"
Content-Type: image/jpeg
�����ExifII* ����(1�2�i��Research In MotionBlackBerry 9105HHRim Exif Version1.00a2010:12:09 17:03:59 ��n�0220�v���� � ��� �2010:12:09 17:03:59��� $ &&$ #"(-:1(+6+"#2D36;=@A@'0GLF?K:?@> >)#)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>��!������ }!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������� w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������� ?�`��ⳓ5��f)¤b��a�BS�Sb�)Xz�֝�q�"s�K�PA���}F7&��Vm��GӬ��%]� Uҵ�Z7��h�`�@&i ��i��MKB�P��r���-�B|ϛ=3Yٶ ��
,字段将如下
-----------------------------17901701330412
Content-Disposition: form-data; name="parent"
clientphotos
所示字段很容易,获取图像内容也很容易,但是将其保存到数据库中以便我可以将其作为图像读回,却不是那么容易。
我已尝试 byte[] data = Encoding.UTF8.GetBytes(rawdata);
但输出不正确。
有谁知道如何获取图像内容并将其保存到 byte[]
中吗?
更新
第一行是从 context.Request.Files[name].InputStream.Read(data, 0, fs);
获取图像
第二行是使用 Encoding.UTF8.GetBytes (rawdata);
第三行使用 Encoding.ASCII.GetBytes(rawdata);
显然第一行是正确的并且有效。
现在我只是使用第一行来获取结果,除非有人可以教,否则它可能会保持这种状态我如何从输入流中读取它。
更新
找到了一个分享代码的好地方 源代码 问题出在第 49 行,目前只读取 <代码>请求.文件
Trying to upload a form with a file input, only getting the form fields from the input stream and not the File.Request method.
The form contains a file input and a couple of text boxes so I cant just upload the stream to the database.
I convert to string using this method
int len = Convert.ToInt32(context.Request.InputStream.Length);
byte[] stream = new byte[len];
context.Request.InputStream.Read(stream, 0, len);
string mime = Encoding.UTF8.GetString(stream);
and then split the multipart/form-data at the boundaries and read the first line of each part to see if its a file or not. You can see the full code Here
A file will look something like this
-----------------------------17901701330412
Content-Disposition: form-data; name="file"; filename="IMG00004-20101209-1704.jpg"
Content-Type: image/jpeg
�����ExifII* ����(1�2�i��Research In MotionBlackBerry 9105HHRim Exif Version1.00a2010:12:09 17:03:59 ��n�0220�v���� � ��� �2010:12:09 17:03:59��� $ &&$ #"(-:1(+6+"#2D36;=@A@'0GLF?K:?@> >)#)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>��!������ }!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������� w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������� ?�`��ⳓ5��f)¤b��a�BS�Sb�)Xz�֝�q�"s�K�PA���}F7&��Vm��GӬ��%]� Uҵ�Z7��h�`�@&i ��i��MKB�P��r���-�B|ϛ=3Yٶ ��
and a field will look something like this
-----------------------------17901701330412
Content-Disposition: form-data; name="parent"
clientphotos
Parsing the field is easy, and getting the image content is easy, but then saving that in to the database so i can read it back out as an image, isnt so easy.
I have tried byte[] data = Encoding.UTF8.GetBytes(rawdata);
but the output isnt correct.
Has anyone any ideas how to take the image content and save it to a byte[]
as it should be?
UPDATE
The first line is from getting the image from context.Request.Files[name].InputStream.Read(data, 0, fs);
The second line is using Encoding.UTF8.GetBytes(rawdata);
The third line is using Encoding.ASCII.GetBytes(rawdata);
Obviously the first line is correct and works.
For now Im just using the first line to get the result and it'll probably stay that way unless someone can teach me how to read it from the input stream.
UPDATE
Found a nice place to share the code Source Code The trouble is on line 49 which for now just reads the Request.Files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应将图像存储为文本,而应以其原始二进制形式存储。如果您必须将二进制数据存储为文本,则需要使用 Base64 对其进行编码,但它会比需要的大得多。
http://www.vbforums.com/showthread.php?t=287324
您还需要执行相反的操作才能将其恢复为可用的图像格式。更好的解决方案是将二进制图像存储在数据库中的 blob/二进制字段类型中。
编辑:只需重读您的帖子,我看到您的数据已经采用 Base64 格式,只需存储在数据库中即可?
再次编辑:最初的问题已被编辑多次,我相信我回答了该问题,但现在问题已通过多次迭代发生了变化,我最初的答案现在不太适用。
You shouldn't store the image as text, store in its raw binary form. If you HAVE to store binary data as text you will need to encode it with Base64, but its going to be alot bigger than it needs to be.
http://www.vbforums.com/showthread.php?t=287324
You will also need to do the reverse to get it back into a usable image format. A much better solution would be to store the binary image in a blob/binary field type in the database.
EDITED: Just reread your post and I see your data is already in base64, just store than in the database?
EDITED AGAIN: The Initial Question has been edited a number of times, I believe I answered the question but now the question has changed through a number of iterations and my initial answer is now less applicable.
不要以字符串形式获取内容。理想情况下,您应该有一个
varbinary(max)
列来存储这些数据(还有一个我之前在 SQL Server 2008 中没有尝试过的文件存储选项)。以下是将数据读入byte[]
并存储在 SQL Server 中的方法:就 Opera 而言,我不熟悉您提出的问题。也许您可以发布一个链接来解释“Opera 必须使用
FileReader
以 base64 进行上传”的含义。如果您仍然需要处理 Base64 字符串,可以将其转换为byte[]
:Don't get the content as a string. Ideally, you should have a
varbinary(max)
column to store this data (there's also a file storage option that I haven't tried before in SQL Server 2008). Here's how you'd read the data into abyte[]
to store in SQL Server:As far as Opera goes, I'm not familiar with the issue you brought up. Maybe you could post a link explaining what you mean by "Opera has to upload in base64 using
FileReader
." If you do still have to process a Base64 string, here's how you'd convert it to abyte[]
: