hi try this code..
SqlConnection con = new SqlConnection
(ConfigurationManager.AppSettings["db"]);
protected void Button1_Click(object sender, EventArgs e)
{
FileInfo imageinfo = new FileInfo(File1.Value.Trim());
if (!imageinfo.Exists)
this.RegisterStartupScript("alertMsg", "<script>alert
('please Select one file to upload');</script>");
else
{
switch (imageinfo.Extension.ToUpper())
{
case ".PDF": this.UpLoadImageFile(imageinfo); break;
case ".DOC": this.UpLoadImageFile(imageinfo); break;
default: this.RegisterClientScriptBlock("alertMsg",
"<script>alert('file type error.');</script>"); break;
}
}
}
private void UpLoadImageFile(FileInfo info)
{
SqlConnection ObjConn = null;
SqlCommand ObjCom = null;
try
{
byte[] content = new byte[info.Length];
FileStream imagestream = info.OpenRead();
imagestream.Read(content, 0, content.Length);
imagestream.Close();
ObjConn = new SqlConnection
(ConfigurationManager.AppSettings["db"]);
ObjCom = new SqlCommand("Insert jayanti_file
(Description,Filename,Picture,Type) values(@Filename,@Picture,@type)",
ObjConn);
ObjCom.Parameters.Add("@Filename",
SqlDbType.NVarChar).Value = System.IO.Path.GetFileName
(File1.PostedFile.FileName);
ObjCom.Parameters.Add("@type", SqlDbType.NVarChar).Value =
System.IO.Path.GetFileName(File1.PostedFile.ContentType);
SqlParameter pictureParameter = new SqlParameter
("@Picture", SqlDbType.Image);
pictureParameter.Value = content;
ObjCom.Parameters.Add(pictureParameter);
ObjConn.Open();
ObjCom.ExecuteNonQuery();
ObjConn.Close();
Button1.Attributes.Add("OnClick", "javascript: return
confirm('Do you want to Upload File.Confirm?');");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
ObjConn.Close();
}
}
On Feb 9, 10:22 am, Cerebrus <[email protected]> wrote:
> Yes, I read those articles before I posted links to them. I realized
> that they were exactly what you needed... save the byte array of a
> file directly to the database.
>
> While I do not personally recommend this approach because you are
> ignoring the primary paradigm of application security - "All input is
> evil until proven otherwise !" Most developers like to run an
> antivirus check at the very least on the file before saving it
> anywhere. Secondly, I also do not recommend saving files into the DB.
> (One of the articles I linked to also reiterates this point)
>
> Nevertheless, it is what you wanted.
>
> And you're welcome. :-)
>
> On Feb 9, 4:10 am, Pete <[email protected]> wrote:
>
>
>
> > Hi Cerebrus,
>
> > Thanks for your help . . .
>
> > Actually, I don't want to really save the file on the server and I was
> > confused how to get the stream without doing this. However, one of
> > your links has enlightened me on how to do this. The code below is
> > what I need:
>
> > HttpPostedFile myFile = FileUpload1.PostedFile;
> > int nFileLen = myFile.ContentLength;
> > byte[] myData = new byte[nFileLen];
> > myFile.InputStream.Read(myData, 0, nFileLen);
>
> > All I want to do is get the byte array of the file in question and
> > then load it into the database. The above code will get the byte
> > array and I can do the rest. I just wasn't aware of how to do this
> > but now I know thanks to you. . .
>
> > Thanks so much for your help,
>
> > Pete
>
> > On Feb 8, 1:10 am, Cerebrus <[email protected]> wrote:
>
> > > I have a feeling that these articles might be helpful to you:
>
> > >http://www.codeproject.com/KB/aspnet/fileupload.aspxhttp://www.aspcod...Hide
> > > quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -