A bloody case caused by a low-level error

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, create a folder for storing images under the application directory (which has been deleted before release), and grant the write permission to the folder. The result is uploaded successfully, it can also be 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 the code I wrote.

The current simple code, in which upload is the image storage directory, is also the place where I create a virtual directory, and spread is its 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.

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 = "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, "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.

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! ");
}

I encountered this strange problem due to my negligence, but why did this happen. I conducted further research.

Server. mappath Method

Returns the physical file path corresponding to the specified virtual path on the Web server.

If you can obtain the physical path of the Directory through this method

Server. mappath ("~ /Spread ") +"/upload "and server. mappath ("~ /Spread/upload. Why is it different? The actual result is that the paths of the two are not the same.

The former returns the original path of upload, and the latter returns the virtual path pointed to by upload. If so, the question has been uncovered.

So I made a small test program.

Test procedure

Create a test case, create the BBS directory under the root directory, and then create the upload directory under the BBS directory. After the test case is released, first test the returned results, and then create a virtual directory for upload, point to another physical path to view the returned value.

Main Program: 

Protected void button#click (Object sender, eventargs E)
{
Label1.text = context. server. mappath ("~ /BBS/") +" Upload ";
Label2.text = context. server. mappath ("~ /BBS/upload ");
}

Test results:

The results confirm the conclusion that, indeed, server. mappath returns the physical address of the virtual path. Ah, in fact, its definition is like this, but sometimes it will not touch the south wall.

But there is another problem:

When a successfully uploaded image is displayed, the path is bound to a relative path and displayed using an image control. The image address is shown as follows: "~ /Spread/upload/080826094153.jpg ", but when you put the image in a virtual directory, you can delete the previous upload directory and display it normally. mappath. This problem confused me again. Does IIS automatically recognize the address of its virtual directory? But if so, why is it not identified during the upload?

Thank you for your comments. The problem has been resolved.

Lessons learned:

<1> correct understanding of basic methods

<2> when uploading files, if you need to create a virtual directory for the storage directory, the server. mappath parameter should be the directory.

<3> be sure to pay attention to the page cache troubles.

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.