SoFunction
Updated on 2025-03-08

Method Precautions Page 1/2

When building a customer promotion system, there is a template management module inside, which requires the administrator to add templates, including the name, description and thumbnails of the template. I used a more traditional method to upload pictures here to upload them, and there was no problem with the test. But after I publish, I create a virtual directory for the folder where the image is stored and grant the directory permission to write, but when I upload the image, it always fails. I have never encountered this situation before and I think it is very strange, so I try my best to solve it.

First, check the permissions for uploading the directory. I added the write and modify permissions of the NetWork Service user, but the result still failed. Then I set the permissions to EveryOne or failed. It seems that it is not a problem with permissions.

Then, delete the virtual directory, and re-create the folder where the picture is stored in the application directory (it has been deleted before publication) and give it write permissions. As a result, the upload is successful and it can be displayed normally.

Then, I copied the uploaded image to the original virtual directory and re-created the virtual directory, and the result was successful.

After these inspections and conceptual work, I summarized that the file can be uploaded and displayed normally. It seems that there is a problem with the conversion between the directory and the virtual directory during uploading, and then I continued to review the code I wrote.

The current short code, where Upload is the storage directory of the picture, and it is also where I create the virtual directory, and Spread is its upper directory. I suddenly realized that it was not pointing to the root directory of the image storage, and it felt a little strange, so I made changes.
Copy the codeThe code is as follows:

string fullName = this.;
string type = (('.') + 1);
if (!(()))
{
(, "Only upload pictures in JPEG, JPE, GIF, BMP, and PNG formats!");
return;
}
string fileName ="Upload/"+ ("yyMMddhhmmss") + "." + type;
try
{
string temp = ("~/Spread");
this.(temp + "/" + fileName);
this. = "~/Spread/" + fileName;
this. = true;
}
catch
{
(, "Uploading the file failed!");
}

The modified code is as follows, and the results can be uploaded and displayed normally. It seems that the problem is indeed here.
Copy the codeThe code is as follows:

string fullName = this.;
string type = (('.') + 1);
if (!(()))
{
(, "Only upload pictures in JPEG, JPE, GIF, BMP, and PNG formats!");
return;
}
string fileName = ("yyMMddhhmmss") + "." + type;
try
{
string temp = ("~/Spread/Upload");
this.(temp + "/" + fileName);
this. = "~/Spread/Upload/" + fileName;
this. = true;
}
catch
{
(, "Uploading the file failed!");
}

12Next pageRead the full text