Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

c# - 文件上载ASP.NET MVC 3.0(File Upload ASP.NET MVC 3.0)

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011 , it is not about ASP.NET Core 3.0 which was released in 2019)

((前言:这个问题是关于2011年发布的 ASP.NET MVC 3.0,它不是关于2019年发布的ASP.NET Core 3.0 ))

I want to upload file in .

(我想在上传文件。)

How can I upload the file using html input file control?

(如何使用html input file控件上传input file ?)

  ask by user637197 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't use a file input control.

(您不使用文件输入控件。)

Server side controls are not used in ASP.NET MVC.

(ASP.NET MVC中不使用服务器端控件。)

Checkout the following blog post which illustrates how to achieve this in ASP.NET MVC.

(查看以下博客文章 ,其中说明了如何在ASP.NET MVC中实现此目的。)

So you would start by creating an HTML form which would contain a file input:

(因此,您首先要创建一个包含文件输入的HTML表单:)

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

and then you would have a controller to handle the upload:

(然后你会有一个控制器来处理上传:)

public class HomeController : Controller
{
    // This action renders the form
    public ActionResult Index()
    {
        return View();
    }

    // This action handles the form POST and the upload
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");        
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...