Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,

i am using this code to save data in server(godaddy) but it is not saving image in my folder of this application so anyone can help me please,

C#
[WebMethod]
   public static string SaveImageData(string value, string remain_data)
   {
       string fetchedData = value.Substring(value.IndexOf(',') + 1);
       string str_ins_result = "";
       //string strpath = "E:\\Reconciliation_Files\\Recon_Files\\" + voID.Substring(2, 2) + "\\" + voID.Substring(4, 2) + "\\" + voID;
       string strpath = "~\\eAHD";

       try
       {
           string[] methodDetails = fetchedData.Split(',');

           string strID_Type_fileName = methodDetails[methodDetails.Length - 1];
           string extension = strID_Type_fileName.Substring(strID_Type_fileName.LastIndexOf('.') + 1);

           string fileContent = fetchedData.Substring(0, fetchedData.LastIndexOf(','));

           byte[] data = Convert.FromBase64String(fileContent);
           MemoryStream ms = new MemoryStream(data);


           if (!Directory.Exists(strpath))
           {
               Directory.CreateDirectory(strpath);
           }

           // instance a filestream pointing to the
           // storage folder, use the original file name
           // to name the resulting file

           FileStream fs = new FileStream(strpath + "\\" + strID_Type_fileName, FileMode.Create);

           string name_path = fs.Name.ToString();

           str_ins_result = save_school_data(name_path, remain_data);
           // write the memory stream containing the original
           // file as a byte array to the filestream

           try
           {
               ms.WriteTo(fs);
               ms.Close();
               fs.Close();
               fs.Dispose();

               return str_ins_result;
           }
           catch (Exception ex_attachSave)
           {
               return "Exception raised while file saving, Exception is : " + ex_attachSave.Message;
           }
       }
       catch (Exception exp)
       {
           //Response.Write(exp.ToString());
           return "Exception raised while file uploading, Exception is : " + exp.Message;
       }
   }
Posted
Comments
Praveen_P 7-Dec-15 10:01am    
do you check whether you have write permission to this folder

1 solution

Try changing strpath:
C#
string strpath = "~\\eAHD";
To:
C#
string strpath = Server.MapPath("~\\eAHD");
'~' is an IIS reference for "root of my website" and isn't directly applicable to file IO methods.
I'd also suggest that you use Path.Combine instead of string concatenation:
C#
FileStream fs = new FileStream(Path.Combine(strpath, strID_Type_fileName), FileMode.Create);
 
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