Last post Jan 21, 2017 05:07 AM by Cathy Zou
Jan 16, 2017 11:01 AM|cyrussullivan|LINK
Ok, I kind of spent the past 4 plus years away from computers due to circumstances beyond my control. Now it seems every time I try to apply old code from back in the day (2012, .net 3.5) to new projects I run into weird errors that make no sense. For instance,
I have a script that always worked for saving files to the file system during the FormView ItemInserting event. Today I got, for the first time, an error that my file had not been saved to the folder. This makes no sense. I am the only user of this machine
and am by default the admin. The folder has full permissions according the the properties, yet, the FileUpload control does not seem to upload anything. I get the error "Could Not Find Part of the Path...myprojectname/uploads/images/testpicture.png". The path
is correct, but the image has not been saved there.
The code I have always used and always worked when I was developing stuff years ago was:
FileUpload FileUpload = (FileUpload)FormView1.FindControl("imageFileUpload");
const string PictureDirectory = "~/upload/images/";
string Picture = PictureDirectory + FileUpload.FileName;
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FileUpload.FileName);
int iteration = 1;
Picture = string.Concat(PictureDirectory, fileNameWithoutExtension, "-", iteration, System.IO.Path.GetExtension(FileUpload.FileName).ToLower());
iteration += 1;
e.Values["image"] = Picture;
The error show up on the same line as FileUpload.SaveAs(Server.MapPath(Picture));
Obviously the picture is not in the directory where it should have been saved. Is it me, something new in .net 4.5, or maybe is Windows 10 to blame?
Jan 16, 2017 11:44 AM|PatriceSc|LINK
Double check the value returned by Server.MapPath(Picture). "Could Not Find Part of the Path" usually means that a folder is really wrong in the full path...
Jan 16, 2017 11:09 PM|cyrussullivan|LINK
You sir are a genius. It turns out I had my path misnamed as "upload" instead of "uploads", but don't pat yourself on the back too hard. I bet I could have figured it out with a little more sleep and little less coffee.
Jan 21, 2017 05:07 AM|Cathy Zou|LINK
Congratulations and thanks for your sharing, which will be helpful for others