Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have the requirement to upload the PDF files and the users able to view the files.
Please give me the suggestion where I have to keep/store the files i.e in the database or server application folder.


I need to upload 200 files per quarter. The average of the file size is 1 MB.


I hope you understand this.

Regards
Sheik
Posted
Updated 14-Dec-11 1:02am
v2

put this in folder and save url in database as
C#
protected void btnSub_Click(object sender, EventArgs e)
   {
       try
       {
           string filename = FileUpload1.FileName;

           FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\Uploadform\\" + filename.Trim()));

           string path = "~\\Uploadform\\" + filename.Trim();

           SqlConnection con = new SqlConnection(str);
           cmd = new SqlCommand("Insert into Circular(date,ctype,title,type,url1,url2) values('"+txtdate.Text+"','" + TextBox1.Text + "','" + txttitle.Text + "','" + txttype.Text + "','" + path + "')", con);
           lblinfo.Text = " Uploaded Successfully ";
           cmd.CommandType = CommandType.Text;
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();
       }
       catch (Exception ex)
       {
           Response.Write(ex.ToString());
       }
   }
 
Share this answer
 
u can do this by following way..
protected void upload_Click(object sender, EventArgs e)

    {
        Label2.Visible = true;
        string filePath = FileUpload1.PostedFile.FileName;          // getting the file path of uploaded file
        string filename1 = Path.GetFileName(filePath);               // getting the file name of uploaded file
        string ext = Path.GetExtension(filename1);                      // getting the file extension of uploaded file
        string type = String.Empty;
 
 if (!FileUpload1.HasFile)
        {
            Label2.Text = "Please Select File";                          //if file uploader has no file selected
        }
        else
        if (FileUpload1.HasFile)
        {
            try
            {
                                                     
                switch (ext)                                         // this switch code validate the files which allow to upload only PDF  file 
                {
                    case ".pdf": 
                        type = "application/pdf"; 
                        break;                 
                 
                }
 
                if (type != String.Empty)
                { 
                   connection();
                    Stream fs = FileUpload1.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);                                 //reads the   binary files
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);                           //counting the file length into bytes
                    query = "insert into PDFFiles (Name,type,data)" + " values (@Name, @type, @Data)";   //insert query
                    com = new SqlCommand(query, con);
                    com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1;
                    com.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
                    com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                    com.ExecuteNonQuery();
                    Label2.ForeColor = System.Drawing.Color.Green;
                    Label2.Text = "File Uploaded Successfully"; 
                }
                else
                {
                    Label2.ForeColor = System.Drawing.Color.Red; 
                    Label2.Text = "Select Only PDF Files  ";                              // if file is other than speified extension 
                }
            }
            catch (Exception ex)
            {
                Label2.Text = "Error: " + ex.Message.ToString(); 
            } 
        }
    }


//Add the following code in the view file button click to View uploaded PDF files in GridView


protected void Button2_Click(object sender, EventArgs e)
    {
        connection();
        query = "Select *from PDFFiles";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();

    }

//Add the following code to the Gridview selected index changed event to download the files:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
             connection();
            SqlCommand com =new SqlCommand("select Name,type,data from  PDFFiles where id=@id", con);
            com.Parameters.AddWithValue("id", GridView1.SelectedRow.Cells[1].Text);
            SqlDataReader dr = com.ExecuteReader();

 
            if (dr.Read())
            {
                Response.Clear();
                Response.Buffer =true;
                Response.ContentType = dr["type"].ToString();
                Response.AddHeader("content-disposition", "attachment;filename=" + dr["Name"].ToString());     // to open file prompt Box open or Save file         
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite((byte[])dr["data"]);
                Response.End();
            }

}
 
Share this answer
 
Hi,

You can put it in application folder.When you need to view this, just give the hyperlink to the particular path of pdf document.

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900