使用物理路径中的HttpPostedFileBase.SaveAs保存上载的文件 [英] Saving an uploaded file with HttpPostedFileBase.SaveAs in a physical path

查看:2207
本文介绍了使用物理路径中的HttpPostedFileBase.SaveAs保存上载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过方法 HttpPostedFileBase.SaveAs()将上传的文件保存到物理路径。

I'd like to save an uploaded file to a physical path by the method HttpPostedFileBase.SaveAs().

当我选择物理路径时,会出现一个异常,表明路径必须是虚拟的。

When I choose a physical path, an exception appears indicates that the path must be virtual.

 var fileName = Path.GetFileName(fileurl.FileName);
 var path = "C:/Projets" + fileName;
 fileurl.SaveAs(Server.MapPath(path));

如何更改我的代码以便能够在我想要的地方保存文件?

How can I change my code to be able to save the file every where I want?

推荐答案

Server.MapPath 仅适用于属于网站的物理位置。如果要将文件保存在外面,可以使用以下命令:

The Server.MapPath works only with physical locations that are part of the website. If you want to save the file outside you could use the following:

var fileName = Path.GetFileName(fileurl.FileName);
fileurl.SaveAs(Path.Combine(@"c:\projects", fileName));

确保执行应用程序池的帐户被授予对此文件夹的写入权限。

Make sure though that the account under which your application pool is executing is granted write permissions to this folder.

这篇关于使用物理路径中的HttpPostedFileBase.SaveAs保存上载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆