hi dear all
me doing file uploading application using asp.net2.0.i am trying to
upload the file to database.its working fine in local system.and when
i checked in server its asking that.select file to upload.
here is my code please help me to do the same

protected void Button1_Click(object sender, EventArgs e)
    {
        FileInfo imageinfo = new FileInfo(File1.Value.Trim());
      //  Response.Write(imageinfo);


        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;
                case ".ZIP": this.UpLoadImageFile(imageinfo); break;
                case ".JPG": this.UpLoadImageFile(imageinfo); break;
                case ".GIF": this.UpLoadImageFile(imageinfo); break;
                case ".BMP": 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["jayanti"]);
            ObjCom = new SqlCommand("Insert
jayanti_file(Description,Filename,Picture,Type)
values(@Description,@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);


            ObjCom.Parameters.Add("@Description",
SqlDbType.NVarChar).Value = this.TextBox1.Text.Trim();
            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();
        }
    }

Reply via email to