使用串联字符串在Web API中创建图像文件夹

时间:2018-10-31 16:46:10

标签: c# .net api asp.net-web-api

我是c#的新手,这里我试图形成一个URL来将图像存储在API本身中。

我要在以下结构中创建一个文件夹

图像文件夹->水果文件夹->无核文件夹-> Image.jpg

编码:

string imageURL = HttpContext.Current.Server.MapPath("~/Images/");

folderPath = imageURL + formData.RootFolder + formData.TypeFolder + "/";

filePath = (folderPath + postedFile.FileName);

postedFile.SaveAs(filePath);

通过使用上面的代码,它将图像存储在以下结构中。

图片-> Fruits_FolderSeedless_Folder-> Image.jpg

在调试代码时,我可以看到如下的URL格式

“ D:\ Projects \ Dot Net \ FruitsDisplay \ FruitsDisplaySolution \ Images \ FruitsSeedless /”

例如:图片->水果无籽-> Image.jpg

但我希望它应为图片->水果->无籽-> Image.jpg

谁能帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

让操作系统决定对子目录使用什么,因为它不一定总是熟悉的\字符。使用Path.Combine()方法将使用在该环境中有效的字符:

folderPath =Path.Combine(imageURL,formData.RootFolder, formData.TypeFolder, postedFile.FileName);