文件上传到服务器 [英] Upload files to server

查看:146
本文介绍了文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做我的第一个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(「指数」);
}

基本上我得到的错误是:


  

http://example.com/Sites/mvc/classifieds/Images/slider < /一>不是一个有效的虚路径。


下面是查看:

 &LT;表ID =CONTACT_FORM方法=邮报行动=/分类/ CreateAnuncioENCTYPE =的multipart / form-data的&GT;
    &LT;输入类型=文件名称=thePic/&GT;
    &LT;输入类型=提交值=发送&GT;
 &LT; /表及GT;


解决方案

如果您在同一台服务器上运行您的code,可以使用相对路径到该文件夹​​。

  VAR文件路径= Path.Combine(使用Server.Mappath(〜/分类/图片/滑块),
                                      Path.GetFileName(Nuevo.id + thePic.FileName));

如果是在网络中的共享位置,但在这里你部署了code中的服务器进行访问,这也应该工作

  VAR文件路径= Path.Combine(\\\\ myFileServerName \\文件\\,
                                        Path.GetFileName(Nuevo.id + thePic.FileName));

在这两种情况下,确保该目录的权限被更新,以便ASP.NET可以写的。

右键单击该文件夹,并转至属性 - >安全性并更新权限。

I'm making my first MVC Project for the school and ran into this problem:

I'm building an Classifieds web page where user can upload pictures of what they want to sale, As far as i understand the best practice to to this, is store the Image path into the Db table and the file goes into a file in the server. So with the path i can retrieve that specific image in the web page.

The problem is that i'm able to store the image only in my local computer not in the server where i've published the project.

How can i upload this file to a server instead of my local computer?

This is my controller:

public ActionResult CreateAnuncio( HttpPostedFileBase thePic)
{
    if (thePic != null && thePic.ContentLength > 0)
    {
        // string filePath = Path.Combine(Server.MapPath("http://example.com/Sites/mvc/classifieds/Images/slider"), Path.GetFileName(Nuevo.id + thePic.FileName)); // Does not Work

        string filePath = Path.Combine(Server.MapPath("~/Images/slider/"), Path.GetFileName(thePic.FileName)); //Works only for local saving

        thePic.SaveAs(filePath);
    }
    return RedirectToAction("Index");
}

Basically the error I'm getting is:

http://example.com/Sites/mvc/classifieds/Images/slider' is not a valid virtual path.

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 work

var 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.

这篇关于文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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