0

I have 2 projects: one is a web service project and other is a web app project. I have to upload an image file from web service and save the same image in a folder in web app project. The file path is stored in the database and image itself is stored in a folder in web app project

Below is my code:

try
        {
         
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            
                
               
                string strFolder= Server.MapPath("./");
                int strtPos = strFolder.IndexOf("webservices_ewbms");
                string rootPath = strFolder.Substring(0, strtPos);
                string pathOfEwbms = rootPath+"ewbms\\InspectionPhotos";

                //string path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;

                //string filePath = "~/InspectionPhotos/" + Path.GetFileName(str + ".jpeg");
                //File.WriteAllBytes(Server.MapPath(filePath), imageBytes);
                //insertInsPhotoCmd.Parameters.AddWithValue("@Photos", filePath);
                File.WriteAllBytes(Server.MapPath(pathOfEwbms), imageBytes);
                insertInsPhotoCmd.Parameters.AddWithValue("@Photos", pathOfEwbms);
                insertInsPhotoCmd.ExecuteNonQuery();
            }


            tran.Commit();
            status = "true";
            response = "{ \"status\":\"" + status + "\",\"data\":\"Inspection Details Added Successfully \"}";

        }

I am able to save the file in web service folder, but I am not getting any idea on how to save the image in a folder in web app. The web app project and web service project are present in the same folder. Please help!

Edit: I have managed to create the physical path of the destination folder of web app, but I am getting an error: E:/New folder (2)/ewbms/InspectionPhotos' is a physical path, but a virtual path was expected. How can I get the virtual path to get the image save at required folder.

  • 1
    You just have to use the path to wherever you want to save it. Of course the process identity must have access but that's not a programming issue. By the way, have you considered what will happen when someone uploads a file with a name that is already there? – Crowcoder Dec 07 '22 at 11:51
  • 1
    If you are using the `HttpPostedFileBase`, then you can use the saveAs() method to save it in the folder that you want to. You should restrict the content length to prevent malicious uploads. – Anand Sowmithiran Dec 07 '22 at 12:10
  • @AnandSowmithiran @Crowcoder I have managed to create a physical path, but I am getting error which says that : `'E:/New folder (2)/ewbms/InspectionPhotos' is a physical path, but a virtual path was expected.` How can I get the virtual Path ? – Amit Kaushal Dec 08 '22 at 10:21
  • 1
    your variable `pathOfEwbms` is not being framed correctly. Already you did apply MapPath, so again during WriteAllBytes no need to call MapPath. Btw, WriteAllBytes expects the full file path, which means your filename and extension included. – Anand Sowmithiran Dec 08 '22 at 10:45
  • @amit-kaushal Hope with my above comment you solved your issue? – Anand Sowmithiran Dec 10 '22 at 08:54
  • Glad to know, for closure, I have added as an answer, you can accept it. – Anand Sowmithiran Dec 12 '22 at 12:13

1 Answers1

1

In your code, the variable pathOfEwbms is not being framed correctly. Already you did apply MapPath, so again during WriteAllBytes no need to call MapPath. The usage of MapPath twice caused this error : ...is a physical path, but a virtual path was expected.

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22