You must Sign In to post a response.
  • Category: ASP.NET

    Upload .pdf,.excel,.xlsx,.doc. files

    Hello Developers,
    I want to upload .pdf, .excel, .xlsx, .doc. type files.
    so help me how to upload different types of files.

    Thanks with,
    Paul.S
  • #762501

    Hi

    You can try this code for fileupload

    Client Side Code


    <asp:FileUpload ID="FUP1" runat="server" />
    <asp:Button ID="BtUPLOAD" runat="server" Text="Upload" OnClick="BtUPLOAD_Click" />



    Server Side Code C#



    protected void BtUPLOAD_Click(object sender, EventArgs e)
    {
    string ext = System.IO.Path.GetExtension(FUP1.PostedFile.FileName);
    if(ext==".pdf" && ext==".xlsx" && ext==".xls" && ext==".doc")
    {
    // Your Code here
    }
    else
    {
    // Your Code here
    }
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762502
    Thanks mr.Developer Thanks a lot.

    Paul.S

  • #762503
    Hi

    Paul

    Welcome . Good select as best answer.

    Share your knowledge post more issue in our website we can expect quick reply.

    Post more gain knowledge lot of things.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762504
    i want to insert files that have having extension of .xls,.xlsx,.pdf,.doc now uploading is working well. but my condition is not working well, if i give the if condition na i can,t upload any type of file. so am having any error in my code anyone help me to i am clear it.

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
    string exten = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    string exten1 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);

    if (exten == ".pdf" || exten == ".xlsx" || exten == ".xls" || exten == ".doc")
    {
    if (exten1 == ".jpeg" && exten1 == ".png")
    {
    OleDbConnection con = Connection.DBconnection();
    OleDbCommand com = new OleDbCommand("insert into tbl_enquiry (Contactname,Companyname,Designation,address,city,country,emailid,phone,fax,mobile,enquiry,photo,attachedfile,dt,status)values(@Contactname,@Companyname,@Designation,@address,@city,@country,@emailid,@phone,@fax,@mobile,@enquiry,@photo,@attachedfile,dt,'A')", con);
    com.Parameters.AddWithValue("@Contact name", txtname.Text.Trim());
    com.Parameters.AddWithValue("@Company name", txtcompanyname.Text.Trim());
    com.Parameters.AddWithValue("@Designation", txtdesign.Text.Trim());
    com.Parameters.AddWithValue("@address", txtaddress.Text.Trim());
    com.Parameters.AddWithValue("@city", txtcity.Text.Trim());
    com.Parameters.AddWithValue("@country", txtcountry.Text.Trim());
    com.Parameters.AddWithValue("@Emailid", txtmail.Text.Trim());
    com.Parameters.AddWithValue("@phone", txtphone.Text.Trim());
    com.Parameters.AddWithValue("@fax", txtfax.Text.Trim());
    com.Parameters.AddWithValue("@mobile", txtmobile.Text.Trim());
    com.Parameters.AddWithValue("@enquiry", txtenquiry.Text.Trim());
    com.Parameters.AddWithValue("@attachedfile", File2.ToString());
    com.Parameters.AddWithValue("@attachedfile", SqlDbType.VarChar).Value = File1.ToString();
    com.Parameters.AddWithValue("@dt", enquiry_date.ToString());
    com.ExecuteNonQuery();

    lblmessage.Text = "Inserted Successfully";
    }

    else
    {
    lblmessage.Visible = true;
    lblmessage.Text = "Please Upload only having Extension jpeg or png formats";
    }
    else
    {
    lblmessage.Visible = true;
    lblmessage.Text = "Please Upload only having Extension pdf or doc or xlsx or xls Format";
    }

    }
    }

    Sorry for the inconvenience.

    Thanks with
    Paul.S

  • #762507
    Hi Paul Raj,

    If you want to improve your knowledge this is not the correct way to improve your skills, I told you no of times first of all you should try by your own.

    split up the requirements into small small pieces and then eat it.

    As per your post, where you need to save the uploaded files, Is it in database binary format.? or any CMS location..? etc... you need to elaborate those points; and make sure before asking question you should try it by your self, whatever dotnetdeveloper posted answer that is basic thing if you search in Google you may found lakhs of records. Post the question after you fail to search it in Google, then only you can improve your coding skills.

    coming to your comment # 4, check the extension type, I guess the problem is smaller and capital issue while checking the extension. My suggestion is better to convert the extension either in lower case or upper case and then assign for checking.

    ex:

    if(extn.ToLower()=="jpeg") // in case of lower convertion
    if(extn.ToUpper()=="JPEG")//in case of upper convertion


    Hope you got my point.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762510
    Hi

    You can try this Code for Validate upload Section

    Client Side Code



    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:FileUpload ID="FileUpload2" runat="server" />
    <asp:Button ID="BtUPLOAD" runat="server" Text="Upload" OnClick="BtUPLOAD_Click" />

    <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label>




    Server Side Code c#



    protected void BtUPLOAD_Click(object sender, EventArgs e)
    {

    string exten = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    string exten1 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);

    if (exten == ".pdf" || exten == ".xlsx" || exten == ".xls" || exten == ".doc")
    {
    lblmessage.Text = "Inserted Successfully";
    }
    else
    {
    lblmessage.Visible = true;
    lblmessage.Text = "Please Upload only having Extension pdf or doc or xlsx or xls Format";
    }

    if (exten1 == ".jpeg" && exten1 == ".png")
    {
    lblmessage.Text = "Inserted Successfully";
    }
    else
    {
    lblmessage.Visible = true;
    lblmessage.Text = "Please Upload only having Extension jpeg or png formats";
    }
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762517
    ok , mr.naveen i unserstood your comments, hereafter i do not post like that my previous post, Am sorry.

    Thanks with
    Paul.S


Sign In to post your comments