27
Nov
2008
0

Grab a file from the web and store it in your database using c#

If you want to download a file from the web and store it directly in a kind of blob field in your database

string url = "the url of the file you want to download";
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
	b = br.ReadBytes((int)stream.Length);
        br.Close();
}
myResp.Close();
// say we have a LINQ datacontext named "context", an entity video with a varbinary(max) 
// field named preview to store the content of the file (for instance a picture)
video vid = new video();
vid.preview = b;
context.videos.InsertOnSubmit(vid);
context.SubmitChanges();
Ecrit par julien dans : .NET | Tags : , ,

Propulsé par WordPress | Thème Aeros | TheBuckmaker.com WordPress Themes | Traduction WordPress tuto