开发者

How to convert this to an image to store in SQL

开发者 https://www.devze.com 2023-04-11 06:28 出处:网络
Trying to upload a form with a file input, only getting the form fields from the input stream and not the File.Request method.

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 an开发者_如何学编程d 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.

How to convert this to an image to store in SQL

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


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

byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);    
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;

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.


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 a byte[] to store in SQL Server:

var file = Request.Files[0];
var buffer = new byte[file.ContentLength];
using (var stream = file.InputStream)
{
    var bytesRead = 0;
    while (bytesRead < file.ContentLength)
    {
        bytesRead += stream.Read(
            buffer, bytesRead, file.ContentLength - bytesRead);
    }
}

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 a byte[]:

using (var reader = new StreamReader(context.Request.InputStream))
{
    var base64Str = reader.ReadToEnd();
    var bytes = Convert.FromBase64String(base64Str);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号