简体   繁体   中英

I would like to create a folder to store images in C# web application

//Directory where images are stored on the server
String dist = "~/ImageStorage";

//get the file name of the posted image
string imgName = image.FileName.ToString();

String path = Server.MapPath("~/ImageStorage");//Path

//Check if directory exist
if (!System.IO.Directory.Exists(path))
{
    System.IO.Directory.CreateDirectory(MapPath(path)); //Create directory if it doesn't exist
}

//sets the image path
string imgPath = path + "/" + imgName;

//get the size in bytes that
int imgSize = image.PostedFile.ContentLength;


if (image.PostedFile != null)
{

    if (image.PostedFile.ContentLength > 0)//Check if image is greater than 5MB
    {
        //Save image to the Folder
        image.SaveAs(Server.MapPath(imgPath));

    }

}

I would like to create a directory of folder on the server were the application is, the folder should store all images uploaded by users. the code above is not working:

Server Error in '/' Application. 'c:/users/lameck/documents/visual studio 2012/Projects/BentleyCarOwnerAssociatioServiceClient/BentleyCarOwnerAssociatioServiceClient/ImageStorage' is a physical path, but a virtual path was expected. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 'c:/users/lameck/documents/visual studio 2012/Projects/BentleyCarOwnerAssociatioServiceClient/BentleyCarOwnerAssociatioServiceClient/ImageStorage' is a physical path, but a virtual path was expected.

Source Error:

Line 36: if (!System.IO.Directory.Exists(path)) Line 37: { Line 38: System.IO.Directory.CreateDirectory(MapPath(path)); //Create directory if it doesn't exist Line 39: } Line 40:

Source File: c:\\Users\\Lameck\\Documents\\Visual Studio 2012\\Projects\\BentleyCarOwnerAssociatioServiceClient\\BentleyCarOwnerAssociatioServiceClient\\registerCar.aspx.cs Line: 38

You should not use MapPath() twice for the same path.

Old Code:

System.IO.Directory.CreateDirectory(MapPath(path)); //Create directory if it doesn't exist

Corrected Code:

System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist

EDIT: Also corrected this:

string imgPath = path + "/" + imgName;

To this:

string imgPath = Path.Combine(path, imgName);

whole corrected code:

  //get the file name of the posted image
    string imgName = image.FileName.ToString();

    String path = Server.MapPath("~/ImageStorage");//Path

    //Check if directory exist
    if (!System.IO.Directory.Exists(path))
    {
        System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
    }

    //sets the image path
    string imgPath = Path.Combine(path, imgName);

    //get the size in bytes that
    int imgSize = image.PostedFile.ContentLength;


    if (image.PostedFile != null)
    {

        if (image.PostedFile.ContentLength > 0)//Check if image is greater than 5MB
        {
            //Save image to the Folder
            image.SaveAs(imgPath);

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM