in my webapp I've created and populated a stringbuilder for a csv file开发者_如何学JAVA. Normally I write it directly to the response object for the user to download through the browser. However, now I want to save it to a SQL DB image field. Is there a direct way to stream it in? Or do I have to save it to file first, then read it back in?
TIA folks!
Just pass sb.ToString() to your parameter. It's only text.
First, why do you want to store text data as an image (= binary = byte array) data type?
If you use an n/text or n/varchar(max) column, you can pass the result of the StringBuilder's ToString() method to your insert statement as parameter.
If you really really insist on storing text as a binary field, use the Encoding.GetBytes() method of your preferred encoding to retrieve the byte array.
I assume that you only mention the Stringbuilder and CSV file because that refers to your previous experiences with enabling file downloads to users.
It is a little bit unclear whether you want to receive the image file from the user or provide the image file for the user to download? I have therefore provided the solution to both questions.
How do you return an image from SQL Server with the httpResponse?
- Get the image data from the database
- Set the correct
content-type
of the response (e.g.image/jpg
,image/gif
orimage/png
) - Write the bytes of your image to the raw response stream
Here is a relevant thread with some code samples: How to load an image in ASP.NET from a database as a file in a web browser?
As specified in the preferred answer you should consider implementing an httpHandler to provide the images rather than serving them throug an ASPX-page (which is intended for html)
How do you enable image file upload to a SQL server database?
If you want to save an image file received from a file input control
into a database then you should check out this link:
How do you store a picture in an image column?
精彩评论