问题描述
|
在调试/测试时,我一直试图将传入的上传图像保存到我的开发机上的〜/ Content中,但是没有文件在那里传输。该文件夹是否允许将图像保存/传输到该文件夹,还是需要将它们保存在〜/ App_Data中?
编辑:我正在使用的代码:
public ActionResult(AdminGameEditModel formData)
{
Game game = new Game();
AutoMapper.Mapper.Map<AdminGameEditModel,Game>(formData,game);
if (formData.BoxArt.ContentLength > 0 && formData.IndexImage.ContentLength > 0)
{
var BoxArtName = Path.GetFileName(formData.BoxArt.FileName);
var BoxArtPath = Path.Combine(Server.MapPath(\"~/Content/Images/BoxArt\"),BoxArtName);
game.BoxArtPath = BoxArtPath;
formData.BoxArt.SaveAs(BoxArtPath);
var IndexImageName = Path.GetFileName(formData.IndexImage.FileName);
var IndexImagePath = Path.Combine(Server.MapPath(\"~/Content/Images/GameIndexImages\"),IndexImageName);
game.IndexImagePath = IndexImagePath;
formData.IndexImage.SaveAs(IndexImagePath);
}
// rest of controller method
}
这两个文件都是“ 1”个对象。我当前使用的服务器只是在VS调试期间运行的ASP.NET服务器。
解决方法
您应该能够将上传的文件保存到您选择的任何目录中。所要确保的是确保运行ASP.NET的服务帐户对该文件夹具有写权限。对于IIS 7或更高版本,可能是服务器上的网络服务帐户。可以肯定的是,在IIS中查看您的站点正在其下运行的应用程序池,并检查其正在其下运行的身份。
更新:
我知道了。您可以尝试这样做,以查看该类型的SaveAs方法是否存在问题(我记得有些人对此有问题)。
代替:
formData.BoxArt.SaveAs(BoxArtPath);
尝试:
using (var output = new FileStream(BoxArtPath,FileMode.CreateNew)) {
var data = new byte[formData.BoxArt.ContentLength];
formData.BoxArt.InputStream.Read(data,data.Length);
output.Write(data,data.Length);
}
,您可以在磁盘上的任何位置复制/保存图像和文件。
您只需要检查IIS进程是否可以访问该文件夹。
我倾向于创建一个自定义文件夹,在其中放置上传的文件。