Asp.net server. mappath method note page 1/2

Source: Internet
Author: User

When creating a customer promotion system, there is a template management module in which the Administrator needs to add a template, including the template name, description, and thumbnail. The image uploading function is provided here, I used a traditional method for uploading and testing. However, after I publish an image, I create a virtual directory for the folder that stores the image and grant the write permission to the directory. However, when I upload the image, it always fails. I have never encountered such a situation before, and I think it is very strange. So I try my best to solve it.

First, check the permission of the upload directory. I added the write and modify permissions of the network service user, but the result still failed. Then I set the permission to everyone or not, which does not seem to be a permission issue.

Then, delete the virtual directory andProgramThe directory re-creates the folder for storing images (which has been deleted before release), and grants the write permission to the folder. The results are uploaded successfully and displayed normally.

Next, I copied the uploaded image to the original virtual directory and re-created the virtual directory. The result is displayed successfully.

After these checks and ideas, I have summarized that files can be uploaded and displayed normally. It seems that there is a problem with the conversion between directories and virtual directories during upload, then I continue to review what I wroteCode.

the current simple code, in which upload is the image storage directory, where I create a virtual directory, and spread is the upper directory. I suddenly found that the original server. mappath did not point to the root directory of the image storage, and it felt a little strange, and then it was modified. copy Code the code is as follows: string fullname = This. fileupload2.postedfile. filename;
string type = fullname. substring (fullname. lastindexof ('. ') + 1);
If (! Phototypes. isexist (type. tolower ()
{< br> BMC. clutility. showmessage (this. page, "only images in JPEG, jpe, GIF, BMP, and PNG formats can be uploaded! ");
return;
}< br> string filename =" upload/"+ system. datetime. now. tostring ("yymmddhhmmss") + ". "+ type;
try
{< br> string temp = server. mappath ("~ /Spread ");
This. fileupload2.postedfile. saveas (temp +"/"+ filename);
This. image2.imageurl = "~ /Spread/"+ filename;
This. image2.visible = true;
}< br> catch
{< br> BMC. clutility. showmessage (this. page, "An error occurred while uploading the file! ");
}

The modified code is as follows, and the result can be uploaded and displayed normally. It seems that the problem is indeed here.Copy codeThe Code is 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 images in JPEG, jpe, GIF, BMP, and PNG formats can be uploaded! ");
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, "An error occurred while uploading the file! ");
}

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.