
Como subir una imagen a SQL Server desde Asp.Net
Publicado por Alexis (6 intervenciones) el 21/07/2016 08:05:02
Como subir una imagen a SQL Server desde Asp.Net
Valora esta pregunta


0
CREATE TABLE Products
(
id int NOT NULL ,
name nvarchar (80) NOT NULL,
quantity int NOT NULL,
price smallmoney NOT NULL,
productImage image NULL ,
CONSTRAINT PK_Products PRIMARY KEY CLUSTERED (id)
)
private void browseButton_Click(object sender, System.EventArgs e)
{
// Se crea el OpenFileDialog
OpenFileDialog dialog = new OpenFileDialog();
// Se muestra al usuario esperando una acción
DialogResult result = dialog.ShowDialog();
// Si seleccionó un archivo (asumiendo que es una imagen lo que seleccionó)
// la mostramos en el PictureBox de la inferfaz
if (result == DialogResult.OK)
{
pictureBox.Image = Image.FromFile(dialog.FileName);
}
}
private void browseButton_Click(object sender, System.EventArgs e)
{
// Se crea el OpenFileDialog
OpenFileDialog dialog = new OpenFileDialog();
// Se muestra al usuario esperando una acción
DialogResult result = dialog.ShowDialog();
// Si seleccionó un archivo (asumiendo que es una imagen lo que seleccionó)
// la mostramos en el PictureBox de la inferfaz
if (result == DialogResult.OK)
{
pictureBox.Image = Image.FromFile(dialog.FileName);
}
}
create table myimages
(
imgid int identity(1,1) primary key,
imgname varchar(30) null,
imgfile image not null,
imgtype varchar(30) null
)
private void Button1_Click(object sender, System.EventArgs e)
{
Stream img_strm = upload_file.PostedFile.InputStream;
//Retrieving the length of the file to upload
int img_len = upload_file.PostedFile.ContentLength;
//retrieving the type of the file to upload
string strtype = upload_file.PostedFile.ContentType.ToString();
string strname = txtimgname.Text.ToString();
byte[] imgdata = new byte[img_len];
int n = img_strm.Read(imgdata, 0, img_len);
int result = SaveToDB(strname, imgdata, strtype);
}
private void Button2_Click(object sender, System.EventArgs e)
{
connection.Open();
SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@param", connection);
SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30);
myparam.Value = txtimgname.Text;
byte[] img = (byte[])command1.ExecuteScalar();
MemoryStream str = new MemoryStream();
str.Write(img, 0, img.Length);
Bitmap bit = new Bitmap(str);
Response.ContentType = "image/jpeg";//or you can select your imagetype from database or directly write it here
bit.Save(Response.OutputStream, ImageFormat.Jpeg);
connection.Close();
}
if (this.FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".jpg", ".png", ".gif" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
//guardamos el archivo en una carpeta
FileUpload1.PostedFile.SaveAs(Server.MapPath(FileUpload1.FileName));
// Leemos el archivo y convertimos a btes
byte[] bytes = System.IO.File.ReadAllBytes(Server.MapPath(FileUpload1.FileName));
guardaimagen(bytes);
}
}
}