[英]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.