• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2475
  • Last Modified:

asp.net c# image upload on server

Hello,I am uing ASp.net 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
  • 4
  • 2
1 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;
dontnetsanjayAuthor Commented:

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

           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.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dontnetsanjayAuthor Commented:

okay dthelper is basically our code for database connection string??
dontnetsanjayAuthor Commented:
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..
dontnetsanjayAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now