Mvc - 控制器每次初始化如何在提交之前将上传的文件存储在列表中? [英] Mvc - controller initializes every time how can I store uploaded files in the list before submit?

查看:66
本文介绍了Mvc - 控制器每次初始化如何在提交之前将上传的文件存储在列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个完整的菜鸟问题。所以我使用Dropzone js,在提交表单之前我将一些文件上传到数据库(当然用户可以在提交表单之前删除更新)。我的问题是每次成功更新数据库我还会在我的控制器中添加文件列表,以便在单击提交按钮时使用。我应该将上传的文件存储在控制器中。这样做的最佳做法是什么?



AttachementsC 我在提交表单后想要使用的列表,但它始终为null 。



我尝试了什么:



Hi I have a complete rookie question.So I am using Dropzone js and before submitting my form I upload some files to database (of course user can delete what update before the submit the form). My question is in every succesfull update to database I also add files to list in my controller to be used in when the submit button clicked. Where should I store uploaded files in controller. What is the best practice t do this?

AttachementsC the list I was thinking about to use after submit my form but it is always null.

What I have tried:

public class CreateTicketController
   {
       
       
       private List<Attachment> AttachementsC = new List<Attachment>();}
     public void UploadFiles()
        {
            Response.ContentType = "text/plain";

            string id = "";
            foreach (string s in Request.Files)
            {
                
                var headers = Request.Headers;
                if (string.IsNullOrEmpty(headers["X-File-Name"]))
                {
                    UploadWholeFile(Request,out id);
                }
               
            }
            Response.Write(id);
            
        }
        private void UploadWholeFile(HttpRequestBase request, out string id)
        {
            id = string.Empty;
            for (int i = 0; i < request.Files.Count; i++)
            {
                var file = request.Files[i];
                Attachment cAttachement = new Attachment();
                if (cAttachement.AttachmentActions.fillAttachmentObject(file))
                {
                    id = Convert.ToString(cAttachement.ID);
AttachementsC.Add(cAttachement);
                }
                else
                {
                    id = string.Empty;
                }
            }
        }

推荐答案

某些课程



Some classes

public class FileUpload
{
    public int ID { get; set; }
    public string Filename { get; set; }
}

public class UploadModel
{
    public List<FileUpload> Files { get; set; }
}





视图





The view

@model UploadModel

@if (Model.Files.Any())
{
    <table>
        @foreach (FileUpload file in Model.Files)
        {
            <tr>
                <td>@file.ID</td>
                <td>@file.Filename</td>
            </tr>
        }
    </table>
}

@using (Html.BeginForm("Upload", "Home"))
{
    for(int i = 0; i < Model.Files.Count; i++)

    {

        @Html.HiddenFor(m => m.Files[i].ID)
        @Html.HiddenFor(m => m.Files[i].Filename)
    }
    
    <span>Filename:</span><input type="text" name="filename"/>
    <input type="submit" value="Upload"/>
}





控制器





Controller

[HttpGet]
public ActionResult Index()
{
    UploadModel model = new UploadModel();
    model.Files = new List<FileUpload>();

    return View(model);
}

static int NextID = 1;

[HttpPost]
public ActionResult Upload(UploadModel model, string filename)
{
    if (model.Files == null)
    {
        model.Files = new List<FileUpload>();
    }

    // I'm grabbing the filename direct from the form post, in reality you'll get it via
    // a file upload control etc.

    FileUpload file = new FileUpload();
    file.Filename = filename;

    // here I'm just incrementing the ID using a static variable
    // your ID will come from the database
    file.ID = NextID++;

    model.Files.Add(file);

    return View("Index", model);
}


这篇关于Mvc - 控制器每次初始化如何在提交之前将上传的文件存储在列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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