开发者

Inserting a image from a url into database

开发者 https://www.devze.com 2023-03-08 12:56 出处:网络
Sorry for the supposedly easy question I am trying to insert a picture from a url into the database. Here alb.picture needs to be inserted.

Sorry for the supposedly easy question I am trying to insert a picture from a url into the database. Here alb.picture needs to be inserted.

url = "https://graph.facebook.com/me/albums/?access_token=" + oAuth.Token;
                        json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
                        Albums albms = js.Deserialize<Albums>(json);
               开发者_运维百科         foreach (Album alb in albms.data)
                        {
                            alb.picture = "https://graph.facebook.com/" + alb.id + "/picture/?access_token=" + oAuth.Token;

                            FileStream fs;
                            string sfn = alb.picture;

                            //FileInfo filImage = new FileInfo(sfn);
                            m_lImageFileLength = alb.picture.Length;
                            m_barrImg = new Byte[Convert.ToInt32(m_lImageFileLength)];
               **fs = new FileStream(sfn, FileMode.Open, FileAccess.Read);**   // this is where it gives me an error abt sfn. C:\win...\sfn path not found

                             fs.Read(m_barrImg, 0, System.Convert.ToInt32(m_lImageFileLength));
                             insert_Facebook_Photos(Convert.ToInt64(ui.id), m_barrImg, alb.created_time.ToString(), alb.updated_time.ToString());
                             fs.Close();


     private void insert_Facebook_Photos(Int64 FacebookUserID, byte[] picbyte, string created_time, string updated_time)
            {
                string conString = "Data Source=HOME-590392F5B5\\SQLEXPRESSR2;Initial Catalog=FMM;Integrated Security=True";
                SqlConnection sqlConnection1 = new SqlConnection();
                sqlConnection1.ConnectionString = conString;
                SqlCommand sqlCommand1 = new SqlCommand();
                try
                {

                sqlConnection1.Open();
                //if (sqlCommand1.Parameters.Count == 0)
                {
                    sqlCommand1.CommandText = "INSERT INTO Facebook_Photos(FacebookUserID,Photo,Photo_CreatedDate,Photo_UpdatedDate) values(@ID,@Picture,@createdDate,@UpdatedDate)";
                    sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 64);
                    sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image);
                    sqlCommand1.Parameters.Add("@createdDate", System.Data.SqlDbType.VarChar, 50);
                    sqlCommand1.Parameters.Add("@UpdatedDate", System.Data.SqlDbType.VarChar, 50);
                }
                sqlCommand1.Parameters["@ID"].Value = FacebookUserId;
                sqlCommand1.Parameters["@Picture"].Value = picbyte;
                sqlCommand1.Parameters["@createdDate"].Value = created_time;
                sqlCommand1.Parameters["@UpdatedDate"].Value = updated_time;
                sqlCommand1.ExecuteNonQuery();


            }

**fs = new FileStream(sfn, FileMode.Open, FileAccess.Read);**   // this is where it gives me an error abt sfn. C:\win...\sfn path not found
alb.picture is supposed to be inserted and How do I get the path of this image


            catch (Exception ex)
            {
                throw ex;

            }
            finally
            {
                sqlConnection1.Close();

            }

        }

Thanks Smitha


You don't need to store the file on local harddisk. It's faster and more reliable to read the image into a MemoryStream and write the binary data directly into database.

0

精彩评论

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