如何创建 ASP.NET MVC 控制器操作以便它们接受文件?

发布于 2024-08-14 11:14:24 字数 236 浏览 2 评论 0原文

我整个周末都在为我的网站开发消息系统,以便用户可以在我的网站内部发送和接收消息。

我已经制定了表模式,现在我可以向不同的用户发送基本消息。

现在,我正在处理附件部分。

如何创建我的操作方法以便它们接受文件?理想情况下,我希望允许控制器操作接受任意数量的文件。一旦它们被传递到控制器操作中,我将把它们保存在我的网络服务器上的某个地方。

谁能向我展示一个接受文件作为其参数一部分的控制器操作的示例?

I've been working all weekend on a Messaging system for my website so users can send and receive messages on my site internally.

I have my table schemas worked out, and right now I can send basic messages to different users.

Now, I'm working on the attachments portion.

How can I create my action methods so that they accept Files? Ideally I would like to allow for a controller action that accepts an arbitrary number of files. Once they have been passed into the controller action I'm going to save them somewhere on my webserver.

Can anyone show me an example of a controller action that accepts files as part of its parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑咖 2024-08-21 11:14:24

(错误处理省略。)

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult FilePost()
    {
        for (var i = 0; i < Request.Files.Count; i++)
        {
            var filebase = Request.Files.Get(i);
            string filePath = Server.MapPath(Url.Content("~/Content/UserContent/"));
            var fileName = // whatever
            filebase.SaveAs(Path.Combine(filePath, fileName));
         }

        return RedirectToAction("Something");
    }

(Error handling omitted.)

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult FilePost()
    {
        for (var i = 0; i < Request.Files.Count; i++)
        {
            var filebase = Request.Files.Get(i);
            string filePath = Server.MapPath(Url.Content("~/Content/UserContent/"));
            var fileName = // whatever
            filebase.SaveAs(Path.Combine(filePath, fileName));
         }

        return RedirectToAction("Something");
    }
記柔刀 2024-08-21 11:14:24

我相信您正在寻找的对象是下面的 Request.Files 示例片段。

foreach (string file in Request.Files) {
    var hpf = Request.Files[file];

    if (hpf.ContentLength == 0)
        continue;

    myFileEntity.ContentType = hpf.ContentType;
    myFileEntity.File = hpf.InputStream.ToByteArray();
    myFileEntity.FileName = hpf.FileName;
}

I believe the object you're looking for is Request.Files example snippet below.

foreach (string file in Request.Files) {
    var hpf = Request.Files[file];

    if (hpf.ContentLength == 0)
        continue;

    myFileEntity.ContentType = hpf.ContentType;
    myFileEntity.File = hpf.InputStream.ToByteArray();
    myFileEntity.FileName = hpf.FileName;
}
三寸金莲 2024-08-21 11:14:24

这是一篇关于如何使用 ASP.NET MVC 上传文件的文章

Here is a post on how to Upload file with ASP.NET MVC

别把无礼当个性 2024-08-21 11:14:24

查看我在这篇文章中的回答。

调用控制器 post 方法时遇到问题

Check out my answer in this post.

Having troubles calling a controller post method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文