In doing a customer promotion system, there is a template management module, requires the administrator to add templates, including the template name, description and thumbnails, and so on, upload the image function here, I used a more traditional method, upload, test no problem. But when I released it, I created a virtual directory for the folder where the pictures were stored and gave the directory write permissions, but when I uploaded the pictures, I always failed. Have not encountered this situation before, feel very strange, so do everything possible to solve.
First, check the upload directory permissions, I added the Network service user's write, modify permissions, the result or failure, and then I set the permissions to everyone or failure, it seems not a matter of permissions.
Then, delete the virtual directory and re-establish the folder where the picture was stored in the application directory (deleted before it was published), and give it write permission, and the result is uploaded successfully and can be displayed normally.
Then I copy the uploaded image to the original virtual directory and recreate the virtual directory, and the results show success.
After these checks and assumptions, I summed up, the file can really upload, also can be normal display, it seems to upload the directory and virtual directory conversion between the problem, and then I continue to review the code I wrote.
The current abbreviated code, where upload is the storage directory for the picture, and the place where I created the virtual directory, and the spread is its upper directory. I suddenly found that the original Server.MapPath did not point to the image storage root directory, it feels a bit strange, and then made a change.
Copy Code code as follows:
String fullName = this. FileUpload2.PostedFile.FileName;
String type = fullname.substring (Fullname.lastindexof ('. ') + 1);
if (! Phototypes.isexist (type. ToLower ()))
{
Bmc.CLUtility.ShowMessage (this. Page, "only upload jpeg, JPE, GIF, BMP, png format pictures!" ");
Return
}
String fileName = "upload/" + System.DateTime.Now.ToString ("Yymmddhhmmss") + "." + type;
Try
{
String temp = Server.MapPath ("~/spread");
This. FileUpload2.PostedFile.SaveAs (temp + "/" + fileName);
This. Image2.imageurl = "~/spread/" + fileName;
This. Image2.visible = true;
}
Catch
{
Bmc.CLUtility.ShowMessage (this. Page, "Upload file failed!" ");
}
The revised code is as follows, the results can be normal upload and display, it seems that the problem is here.
Copy Code code as follows:
String fullName = this. FileUpload2.PostedFile.FileName;
String type = fullname.substring (Fullname.lastindexof ('. ') + 1);
if (! Phototypes.isexist (type. ToLower ()))
{
Bmc.CLUtility.ShowMessage (this. Page, "only upload jpeg, JPE, GIF, BMP, png format pictures!" ");
Return
}
String fileName = System.DateTime.Now.ToString ("YYMMDDHHMMSS") + "." + type;
Try
{
String temp = Server.MapPath ("~/spread/upload");
This. FileUpload2.PostedFile.SaveAs (temp + "/" + fileName);
This. Image2.imageurl = "~/spread/upload/" + fileName;
This. Image2.visible = true;
}
Catch
{
Bmc.CLUtility.ShowMessage (this. Page, "Upload file failed!" ");
}
Current 1/2 page
12 Next read the full text