简体   繁体   English

我想创建一个文件夹来将图像存储在C#Web应用程序中

[英]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. 'c:/ users / lameck / documents / visual studio 2012 / Projects / BentleyCarOwnerAssociatioServiceClient / BentleyCarOwnerAssociatioServiceClient / ImageStorage'是物理路径,但是应该使用虚拟路径。 Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 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. 异常详细信息:System.Web.HttpException:'c:/ users / lameck / documents / visual studio 2012 / Projects / BentleyCarOwnerAssociatioServiceClient / BentleyCarOwnerAssociatioServiceClient / ImageStorage'是物理路径,但是应该使用虚拟路径。

Source Error: 源错误:

Line 36: if (!System.IO.Directory.Exists(path)) Line 37: { Line 38: System.IO.Directory.CreateDirectory(MapPath(path)); 第36行:if(!System.IO.Directory.Exists(path))第37行:{第38行:System.IO.Directory.CreateDirectory(MapPath(path)); //Create directory if it doesn't exist Line 39: } Line 40: //如果目录不存在,则创建目录第39行:}第40行:

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

You should not use MapPath() twice for the same path. 您不应对同一路径使用MapPath()两次。

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);

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将图像存储在文件夹中,并在Mysql数据库的c#应用程序中检索 - store images in a folder and retrieve in c# application with Mysql database 我想在C#中重新创建以下XAML - I would like to re create the following XAML in C# 我是一个非常出色的C#开发人员,但我从未做过类似的事情; 我将如何创建CRUD应用程序生成器? - I'm a pretty good C# developer, but I've never done something like this; how would I create a CRUD application generator? 在C#中我想比较数据表 - in c# I would like to compare datatables C#:我想像文件路径一样将消息传递给我的表单应用程序,比如控制台应用程序,我该怎么做? - C#: I want to pass messages like a file path to my forms application like a console application, how would I do that? MVC Web应用程序C#中的文件夹选择 - Folder selection in MVC web application c# 我想使用aspx旋转网络上的图像 - I would like to rotate images on the web using aspx 如何使用.net C#Web应用程序创建自动完成功能? - How can I create autocomplete with .net C# Web Application? 我想使用asp.net在我的域下动态创建子域,C#? - I would like to create subdomains Dynamically under my domain using asp.net ,C#? 我如何像在 Notepad/Notepad++ 中一样创建“查找表单”? C# - How would I create a “Find Form” Like in Notepad/Notepad++? C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM