We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How do i handle the attachments list in the Create Action

I have a uploader that handle a list of attachments, i've read about saveUrl action, but that doesn't attend my needs, i don't wanna save the attachments one by one, because i have a lot of informations that need to be checked before saving the attachments.

I need to receive the attachments in my Create action in my controller, do business validations and if everything is ok, i get the list of attachment and let it go to my services and repository.

Is there any way to map the attachments and get them in the controller? 

3 Replies

PO Prince Oliver Syncfusion Team May 16, 2019 10:36 PM

Hi James, 

Thank you for contacting us. 

We have checked your requirement. Uploader post the files to the server handler and create action does not handle post requests. So, you need to create a separate create action for handling post request and map it to the SaveUrl property in the AsyncSettings. Here we have used a controller action with name as Save to handle the uploader’s file post.  Now you can get the list of attachments in the mapped controller action method. Here the variable httpPostedFile hold the all the attachments. Kindly refer to the following code 

[AcceptVerbs("Post")] 
public void Save() 
{ 
    try 
    { 
        if (System.Web.HttpContext.Current.Request.Files.AllKeys.Length > 0) 
        { 
            var httpPostedFile = System.Web.HttpContext.Current.Request.Files["UploadFiles"]; 
            if (httpPostedFile != null) 
            { 
                var fileSave = System.Web.HttpContext.Current.Server.MapPath("UploadedFiles"); 
                var fileSavePath = Path.Combine(fileSave, httpPostedFile.FileName); 
                if (!System.IO.File.Exists(fileSavePath)) 
                { 
                    httpPostedFile.SaveAs(fileSavePath); // the file is saved here 
                    HttpResponse Response = System.Web.HttpContext.Current.Response; 
                    Response.Clear(); 
                    Response.ContentType = "application/json; charset=utf-8"; 
                    Response.StatusDescription = "File uploaded succesfully"; 
                    Response.End(); 
                } 
                else 
                { 
                    HttpResponse Response = System.Web.HttpContext.Current.Response; 
                    Response.Clear(); 
                    Response.Status = "204 File already exists"; 
                    Response.StatusCode = 204; 
                    Response.StatusDescription = "File already exists"; 
                    Response.End(); 
                } 
            } 
        } 
    } 
    catch (Exception e) 
    { 
        HttpResponse Response = System.Web.HttpContext.Current.Response; 
        Response.Clear(); 
        Response.ContentType = "application/json; charset=utf-8"; 
        Response.StatusCode = 204; 
        Response.Status = "204 No Content"; 
        Response.StatusDescription = e.Message; 
        Response.End(); 
    } 
} 

In the above highlighted code, we can check the information in the files and if the conditions are valid then we can continue with the save operation, else we can skip it. 


Let us know if you need any further assistance on this. 

Regards, 
Prince 



WM werner mayer May 17, 2019 06:51 AM

(I work with james, actually i posted that whit his account)

Ok, but with this approach i'm not able to bind attachment information to my model wich will be handled when user click "submit"

I wanna know if there's a way to bind the attachments to the model just like the razor native uploader component does, like 

Having in my vm:
public HttpPostedFileBase[] files 

@Html.TextBoxFor(model => model.files, ""new { @type = "file", @multiple = "multiple" }) -- This works well, i can handle the attachments in the create action.

I tried to do like this: 

@Html.EJS().UploaderFor(model => model.files).Render()

But the component doesn't bind the attachments.



CI Christopher Issac Sunder K Syncfusion Team May 21, 2019 04:39 AM

Hi James/Werner, 

In uploader component, there is no for-control support. Instead, you can store preload files in a model and can bind via Files Property. Files property is used to bind the preloaded files when rendering the component. You can store the preloaded files in model controller and get the value in view by accessing the files property. By using the forEach loop get the model value and perform some operation. 
Please refer the below code snippet 
[controller] 
public ActionResult Index() 
{ 
    UploadFileModel fileModel = new UploadFileModel(); 
    fileModel.Files.Add(new UploaderUploadedFiles { Name = "Nature", Size = 500000, Type = ".png" }); 
    fileModel.Files.Add(new UploaderUploadedFiles { Name = "TypeScript Succinctly", Size = 12000, Type = ".pdf" }); 
    fileModel.Files.Add(new UploaderUploadedFiles { Name = "ASP.NET Webhooks", Size = 500000, Type = ".docx" }); 
    return View(fileModel); 
} 
public class UploadFileModel 
{ 
    public UploadFileModel() 
    { 
        Files = new List<UploaderUploadedFiles>(); 
    } 
    public List<UploaderUploadedFiles> Files { get; set; } 
} 

 [Views] 
@Html.EJS().Uploader("UploadFiles").DropArea(".control-fluid").MinFileSize(10000).Removing("onFileRemove").MaxFileSize(4000000).AsyncSettings(new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = @Url.Content("~/Uploader/Save"), RemoveUrl = @Url.Content("~/Uploader/Remove") }).Files(files => 
{ 
    foreach (var f in Model?.Files) 
    { 
        files.Name(f.Name).Size(f.Size).Type(f.Type).Add(); 
    } 
}) 
 

To know more about preload files please refer below sample and api link 

Thanks, 
Christo 


Loader.
Live Chat Icon For mobile
Up arrow icon