Click here to Skip to main content
15,890,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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;
                }
            }
        }
Posted
Updated 14-Apr-16 4:16am

1 solution

Some classes

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

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


The view

Razor
@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

C#
[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);
}
 
Share this answer
 
Comments
Ermany 14-Apr-16 11:05am    
Thank you for your answer I will try!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900