Click here to Skip to main content
15,903,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used following query to upload a photo


C#
objPhoto.MembershipID = Convert.ToInt32(Session["MembershipID"]);
                 DataTable dt = objPhoto.ViewRecord();
                 string file = imgUpload.FileName;
                 string fname = Session["MembershipID"].ToString();
                 string st = "~/UploadImages/";
                 string str = "-";
                 if (dt.Rows.Count > 0)
                 {
                     imgUpload.PostedFile.SaveAs(Server.MapPath("~/UploadImages/") + fname + str + dt.Rows.Count + Path.GetExtension(file));
                     objPhoto.UploadImages = st + fname + str + dt.Rows.Count + Path.GetExtension(file);
                     objPhoto.ImageID = objPhoto.ViewData();
                     imgPreview.ImageUrl = "~/UploadImages/" + fname + str + dt.Rows.Count + Path.GetExtension(file) + "";
                 }


The above mentioned coding is working. But i used the same coding in another form It shows following error

fileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path.
Posted
Updated 18-Jun-14 2:19am
v2

Check if the file name has reserved words. You won't be able to create files with reserved names.

Use System.IO.Path.GetInvalidPathChars()& System.IO.Path.GetInvalidFileNameChars();
So that you can get full list of invalid charecters which cannot be a valid file name. You need to replace those chars from the filename before you create the file.
 
Share this answer
 
Hello,YOu Can Refer This Is Of MVC But You Can Refer this ...It Might Help You
..
public ActionResult Display(HttpPostedFileBase file)
        {
            if (file != null)
            {
                EmployeeEntities context = new EmployeeEntities();
                string ImageName = System.IO.Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/Images/" + ImageName);

                // save image in folder
                file.SaveAs(physicalPath);

                //save new record in database
                Image newRecord = new Image
                    {
                        fname = Request.Form["fname"],
                        lname = Request.Form["lname"],
                        ImageUrl = ImageName
                    };
                _imgRepo.Insert(newRecord);
                context.SaveChanges();

                PhotoForSingleItem tempmodel = new PhotoForSingleItem();
                tempmodel.Images = _imgRepo.GetEntities().ToList();

                return View("Display",tempmodel);
            }

            return null;

..
This Code Is OF Controller...It Will surely Help You..

In _imgRepo.insert(newrecord)
context.savechanges();////You Can Write Your Insert Code ..
But Main Part Is Of PhysicalPath..
Thanks
 
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