Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How could i save address of file in a database and file in a folder and only name of file is shown in database,.
Posted
Comments
sravani.v 7-Oct-11 0:14am    
Do you want to store File address and file name in separate columns of database or not.please clarify ur question?

I will save in ArrayList in the file name of one folder.You can save the ArrayList in your database.You can check the following code.

//FIND ALL FILES IN FOLDER
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Server.MapPath("~/FolderName"));
ArrayList arrFilesName = new ArrayList();
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
   {
   //LOAD FILES
    arrFilesName.Add(f.Name);

   } 


Hope be helpful,
Theingi Win
 
Share this answer
 
C#
if (fileUpload1.HasFile)
           {       
              
                  
                   string FolderName = "Gallery\\";
                   DirectoryInfo thisFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + FolderName);
                   if (!thisFolder.Exists)
                   {
                       thisFolder.Create();
                   }
                   strunFilepath = "~/Resources/" + FolderName + "/" + FileName;
                   filename = "~/Resources/" + FolderName + FileName;
               }


               oHttpPostedFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + strunFilepath.Replace("~", ""));



For saving in the database save 'filename' .
 
Share this answer
 
v4
it works fine for just copy this and use if you face any problem i will tell you if you reply me...

in below code "fuphotos" is the id of my fileupload control

protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
    {
        HttpPostedFile oHttpPostedFile = fuphotos.PostedFile;

       
        string uncFileName = string.Empty;
        string ImageName = string.Empty;
        string StrFileType = string.Empty;
        string strunFilepath = string.Empty;
        string strcFilepath = string.Empty;
        string strcFilepath1 = string.Empty;
        try
        {
            if (fuphotos.HasFile)
            {
               Normal image saving starts
                StrFileType = oHttpPostedFile.ContentType.ToString();
                if (StrFileType == "image/pjpeg" || StrFileType == "image/gif" || StrFileType == "image/jpeg" || StrFileType == "image/bmp")
                {
                    uncFileName = oHttpPostedFile.FileName.Substring(oHttpPostedFile.FileName.LastIndexOf("\\") + 1);
                    string FolderName = "UNC_PhotoGallery\\";
                    DirectoryInfo thisFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + FolderName);
                    if (!thisFolder.Exists)
                    {
                        thisFolder.Create();
                    }
                    strunFilepath = "~/Resources/" + FolderName + "/" + uncFileName;

                }


              oHttpPostedFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + strunFilepath.Replace("~", ""));
              
            }

            string insquery = "INSERT INTO PHOTOS_TABLE(PhotoTitle,PhotoPath,PostedDate) VALUES ('" + txtPhotoTitle.Text + "','" + strcFilepath + "','" + DateTime.Now.ToLongDateString() + "')";
            string result = belObj.Recordinsert(insquery);
            if (result == "Success")
                Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script language='javascript'>fnalert('Photo Uploaded successfully.')</script>");


           
        }
        catch (Exception ex)
        {
          //exception
        }

    }
 
Share this answer
 
v2
Comments
Asad_Iqbal 7-Oct-11 2:49am    
I'll let u know bro, if any problem i face.
Asad_Iqbal 13-Oct-11 11:43am    
bro your answer is limited for pics etc i am doing file sharing site where every file will be uploaded so how could i download it again then

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