0

I am using the following code to save my file into database using entity framework But for some reason it is giving me the error:

'C:/Users/David Buckley/Documents/Visual Studio 2012/Sis/StudentInformationSystem/admin/uploads/' is a physical path, but a virtual path was expected. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 'C:/Users/David Buckley/Documents/Visual Studio 2012/Sis/StudentInformationSystem/admin/uploads/' is a physical path, but a virtual path was expected

But it seems to save the file ok in database as the path and filename of C:\Users\David Buckley\Documents\Visual Studio 2012\Sis\StudentInformationSystem\admin\uploads\test.jpg which exsits but i persume I need to save it differently if I want to load it into an image control imageurl field property?.

       try    

        {

            int id = Convert.ToInt32(Request.QueryString["id"]);

            if (id == -1) // we neeed  a new record otherwise get the old one;
            {

                Student studentRecord = new Student();
                _db.AddStudent(studentRecord);
                _db.SaveChanges();
                newRecordId = studentRecord.Student_ID;
                Session["recordid"] = id;
                _student = _db.GetStudentById(newRecordId);
            }else
                _student = _db.GetStudentById(id);

         photoUpload.TargetFolder= Server.MapPath("~/admin/uploads/");

         string fullPath = Server.MapPath( "~/admin/uploads/");
         photoUpload.OverwriteExistingFiles = true;
         string newFileName = "";
         foreach (UploadedFile file in photoUpload.UploadedFiles)
         {
            string fileName = "test";
             newFileName =fileName + file.GetExtension();
             file.SaveAs(Path.Combine(fullPath, newFileName));
             // impelement your database insert here...
         }
         string thumbPath;
         thumbPath = ("~/images" + "/" + newFileName);
         _student.Image = thumbPath;
    _student.Student_Name = txtStudentName.Text;
    _student.Student_FatherName = txtFathersName.Text;
    _student.Registration_no = txtRegistrationNo.Text;
    _student.Address1 = txtAddress1.Text.Trim();
    _student.Address2 = txtAddress2.Text.Trim();
    _student.Address3 = txtAddress3.Text.Trim();
    _student.RelationWithGuadian = txtRelationshipGurdan.Text.Trim();
    _student.GurdianName = txtGurdianName.Text.Trim();
    _student.LastSchoolAtten = txtLastSchool.Text.Trim();
    _student.Contact1 = txtContact1.Text.Trim();
    _student.Contact2 = txtContact2.Text.Trim();
    _student.DOB = rdDOB.SelectedDate.Value;


          _db.SaveChanges();


        }
       catch (Exception ex)
       {

        }
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • are you trying to access the uploaded file via http using `fullPath` after you upload it? – knobcreekman Mar 27 '15 at 01:24
  • @knobcreekman i am wanting to save it into column in db just the path name and filename so i can show a preview its for a cms i am building at the min I want to show the preview in asp:image but it gives me that error when path that filename to image url property – c-sharp-and-swiftui-devni Mar 27 '15 at 01:36
  • if you are accessing it from a web context, then you will have to use virtual paths. that is what the error is telling you. so it would be something like `http://yourdomain.com/admin/uploads/yourfile.jpg`. so you should only be saving the `~/admin/uploads/yourfile.jpg` in the db. – knobcreekman Mar 27 '15 at 01:49
  • @knobcreekman can you show me how to do that in a answer and ill mark it im working off localhost host at the min for dev thanks – c-sharp-and-swiftui-devni Mar 27 '15 at 02:43

1 Answers1

0

To access the file from a web application, you will need to use a virtual path. But you are saving a physical path to the file in the database. Instead of saving the value of Path.Combine(fullPath, newFileName) in the database, you should be saving the value of "~/admin/uploads/" + newFileName.

The above works as long as your uploads/ directory is a descendant of your application directory. Alternatively, you can use a path that points to a directory outside of your application path by explicitly adding a virtual directory.

How to add a virtual directory in IIS express

How to add a virtual directory in IIS

Community
  • 1
  • 1
knobcreekman
  • 403
  • 2
  • 4
  • 10
  • but thats what im doing and its not saving right in the db i am only using the path of this thumbPath = ("~/images" + "/" + newFileName) but for some reason its saving the c part of the path ?? – c-sharp-and-swiftui-devni Mar 27 '15 at 03:12
  • then there is something else going on in code that you haven't shared. – knobcreekman Mar 27 '15 at 03:28
  • Unless there is some trickery in the setter for `Image` or in `_db.SaveChanges()`, the code you posted would not cause the behavior you describe. Are you by chance trying to use `Server.MapPath(filePathFromDB)` to retrieve the image within your `` control? Have you queried the database directly to verify what value is stored? – knobcreekman Mar 27 '15 at 18:37
  • ImagePreview.ImageUrl = _student.Image; ImagePreview.Visible = true; – c-sharp-and-swiftui-devni Mar 27 '15 at 19:20
  • Have you queried the database directly to verify what value is stored? – knobcreekman Mar 28 '15 at 03:08