asp.net Server.MapPath method notes 1th/2 page _ Practical Tips

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.