Solved c# image upload on server

Posted on 2007-08-10
Last Modified: 2013-11-26
Hello,I am uing with C# ver 2 visual studio 2005 i have a project in which i have to uplaod Images and PDF files save them on SERVER and not dtabase in database only file name will be saved. saved files can be renamed. or resized. user later candelete thru web pages. please tell me which control to add on my aspx for  uploading images and pdf how 2 display them. thank you in advance I am in need for this any help is highly appreciated.

Thanks a ton Sanjay
Question by:dontnetsanjay
    LVL 8

    Accepted Solution

    You can use FileUpload control for this

    some sample code.

    private bool Upload(FileUpload pFileUpload, int pRItemId)
            bool result = false;

            //Grab the file name from its fully qualified path at client
            string strFileName = pFileUpload.PostedFile.FileName;

            // only the attched file name not its path
            string fileName = System.IO.Path.GetFileName(strFileName);
            fileName = pRItemId.ToString() + "_" + fileName;

            //Save uploaded file to server at C:\ServerFolder\
                //string filePath = ConfigurationManager.AppSettings["FilePath"]; //Server.MapPath(Request.Url.AbsoluteUri);
                string filePath = Server.MapPath("GenResources");
                pFileUpload.PostedFile.SaveAs(filePath + @"\" + fileName);
                //Response.Write(filePath + fileName);
                //Update ResourcePath
                //Set source file url in web.config.
                string hostFilePath = ConfigurationManager.AppSettings["FilePath"];
                string urlSourcePath = "http://" + Request.Url.Host + hostFilePath;
                urlSourcePath = urlSourcePath + fileName;
                DataHelper oDataHelper = new DataHelper(Configuration.ConnectionString, "CA_Sp_UpdateResourcePath", CommandType.StoredProcedure);
                oDataHelper.moCommand.Parameters.AddWithValue("@RESOURCEPATH", urlSourcePath);
                oDataHelper.moCommand.Parameters.AddWithValue("@W_RESOURCEITEMSID", pRItemId);

            catch (Exception ex)
                //lblMessage.Visible = true;
                //lblMessage.Text = ex.Message;
            return result;

    Author Comment


    hello again
    thanks for sending complete code this is already a great help which cannot be ignored
    but i am not able to get the thing clearly
    my job is to develop a a module sothat client can enter his product info and upload image or pdf file as cataloge here in this quey i will remian with image upload and pdf i will start another question(chain)
    pRItemId is a variable to asign prefix to the filename?

    dtahelper namespace could not be found??
    in help search also i did not got any result?

    and I am new to all this therefore its not easy for me

    LVL 8

    Expert Comment

               This is just a sample code only don't blindly copy this code, change as per your requirement. Here dtHelper is a custom class that you cannot find any where , that is used in our project. u can write your own code here.

    Author Comment


    okay dthelper is basically our code for database connection string??

    Author Comment

    okay now I am getting thiscode thanks anyway u helped me in this i am editing this code accordingly

     its 12.35 am working on this and post back in few min..

    Author Comment

    thanks a lot my code is working

    io had created a new subfolder in work folder there i have saved it
    i guess i had to use '\' instead of this '/' which took little extra time

    thanks once again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now