本文介绍了文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在做我的第一个MVC项目为学校跑了这个问题:我建立一个公告网页,用户可以上传他们想要出售什么图片,据我了解最佳实践这一点,是存储映像路径到数据库表和文件进入服务器中的文件。这样的路径,我可以检索该网页的特定图像问题是,我能在图像不存储只在我的本地计算机在我已经出版了项目的服务器。 我如何可以将该文件上传到服务器,而不是我的本地计算机?这是我的控制器: 公众的ActionResult CreateAnuncio(HttpPostedFileBase thePic){    如果(thePic =空&放大器;!&放大器; thePic.ContentLength大于0)    {        //串文件路径= Path.Combine(使用Server.Mappath(http://example.com/Sites/mvc/classifieds/Images/slider),Path.GetFileName(Nuevo.id + thePic.FileName)); //不起作用        字符串文件路径= Path.Combine(使用Server.Mappath(〜/图片/滑块/),Path.GetFileName(thePic.FileName)); //仅能用于本地储蓄        thePic.SaveAs(文件路径);    }    返回RedirectToAction(「指数」);} 基本上我得到的错误是:Here is the View:<form id="contact_form" method="post" action="/classifieds/CreateAnuncio" enctype="multipart/form-data"> <input type="file" name="thePic"/> <input type="submit" value="Send"> </form> 解决方案 If you are running your code in the same server, you can use the relative path to that folder. var filePath = Path.Combine(Server.MapPath("~/classifieds/Images/slider"), Path.GetFileName(Nuevo.id + thePic.FileName));If it is a shared location in the network, but accessible from the server where you deployed your code, that should also workvar filePath = Path.Combine(\\myFileServerName\files\", Path.GetFileName(Nuevo.id + thePic.FileName));In both the cases, Make sure that the directory's permission is updated so that ASP.NET can write to that.Right click on the folder and go to properties->Security and update the permissions. 这篇关于文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 13:19