ASP.NET - Como subir una imagen a SQL Server desde Asp.Net

 
Vista:
Imágen de perfil de Alexis

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Como subir una imagen a SQL Server desde Asp.Net

Publicado por Esmeralda (19 intervenciones) el 25/07/2016 20:24:22
Hola primero la base de datos tiene que estar asi
1
2
3
4
5
6
7
8
9
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)
)

en proramacion es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Como subir una imagen a SQL Server desde Asp.Net

Publicado por lorena elideth (10 intervenciones) el 14/08/2016 17:47:23
Hola esto puede ayudarte

1
2
3
4
5
6
7
create table myimages
(
imgid int identity(1,1) primary key,
imgname varchar(30) null,
imgfile image not null,
imgtype varchar(30) null
)


Cuando el usuario hace clic en el botón de subida, un manejador de evento se invoca. En el evento que usted puede agregar el código siguiente al cargar el archivo en la base de datos:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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();
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Como subir una imagen a SQL Server desde Asp.Net

Publicado por jose manuel (9 intervenciones) el 24/08/2016 19:21:16
para subir una imagen desde una ubicacion local, tienes que convertirla en Bytes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
 
 }
                    }
                }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar