MVC文件上传保存所选文件 [英] MVC File Upload save selected file

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

问题描述

我有一个带有文件上传控制功能的表单,它可以按预期工作.但是,在页面上还有其他无效字段的情况下,单击提交按钮并从控制器返回模型后,我很难弄清楚如何保存"所选文件吗? 每当页面上有错误时,用户都必须再次浏览该文件.在控制器上调用POST后保存选择的最佳实践方法是什么?

//VIEW
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input id="inputbox" type="file" name="PageUpload" />
  @Html.TextBoxFor(model => model.otherField1)
  @Html.TextBoxFor(model => model.otherField2)
}

//CONTROLLER
[HttpPost]
public ActionResult Create(MyViewModel view)
{
    try
    { ...
        if (ModelState.IsValid)
        {
            //it is valid save
            return RedirectToAction("Action", "Controller", new { id = view.ModelID });
        }
    }
    catch(Exception e){}
    return View(view);
}

返回View(view)后,我将所有其他字段正确绑定,但没有输入控件.

解决方案

用户需要重新选择文件的原因是一种安全措施.您无法设置文件输入的值(如果可能的话,恶意站点可能会包含几百个带有"C:\ password.doc"等内容的隐藏文件输入,并试图在用户不知情的情况下下载敏感文件).

如果模型无效,则需要先保存文件,然后再返回视图.就我而言,我使用的视图模型包含一个对象,该对象包含文件的显示名称和路径的属性.基本前提是

  1. 将文件保存到一个临时位置
  2. 使用文件路径(到临时路径)更新视图模型 位置)和显示名称
  3. 返回视图
  4. 在视图中,渲染显示名称(以便用户知道哪个文件 已被选中)和Path的隐藏输入.一种 如果用户更改,可以将删除"按钮与此相关联 他们的想法,并希望选择其他文件(使用Ajax删除 临时文件)
  5. 如果发布时模型状态有效,请获取基于以下内容的临时路径 隐藏输入的值,并将文件移至永久 位置,然后保存模型并重定向.

I have a form with file upload control and it works as expected. However I am having trouble figuring out how to 'save' selected file after submit button has been clicked and model returned from the controller in case of some other invalid fields on the page? Every time there are errors on the page, user has to browse for the file again. What is best practice way of saving the selection after calling POST on controller?

//VIEW
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input id="inputbox" type="file" name="PageUpload" />
  @Html.TextBoxFor(model => model.otherField1)
  @Html.TextBoxFor(model => model.otherField2)
}

//CONTROLLER
[HttpPost]
public ActionResult Create(MyViewModel view)
{
    try
    { ...
        if (ModelState.IsValid)
        {
            //it is valid save
            return RedirectToAction("Action", "Controller", new { id = view.ModelID });
        }
    }
    catch(Exception e){}
    return View(view);
}

I get all the other fields correctly bound after returning View(view) but not the input control.

解决方案

The reason the user needs to re-select the file is a security measure. You cannot set the value of a file input (if you could, a malicious site could include a few hundred hidden file inputs with "C:\password.doc" etc. and attempt to download sensitive files without the user knowing).

If the model is invalid, you need to save the file before returning the view. In my case, I use a view model that contains an object that includes properties for the file's display name and path. The basic premise is

  1. Save the file to a temporary location
  2. Update the view model with the files path (to the temporary location) and display name
  3. Return the view
  4. In the view, render the display name (so the user knows which file has already been selected) and a hidden input for the Path. A 'delete' button can be associated with this in case the user changes their mind and wants to select a different file (use ajax to delete the temporary file)
  5. If model state is valid when posting, get the temporary path based on the value of the hidden input and move the file to its permanent location, then save the model and redirect.

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

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