Home > Back-end >  How can I save image file to web app through web service project?
How can I save image file to web app through web service project?

Time:12-14

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.

CodePudding user response:

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.

  • Related