[CLOSED] Refresh grid/store on file upload

  1. #1

    [CLOSED] Refresh grid/store on file upload

    Hi,

    I have a file upload text field, once files are uploaded they should display in a grid panel below. The upload works and when the window is refreshed the file displays in the grid.

    I have tried various methods of refreshing the grid and reloading the store to get the grid to refresh on file upload from both the grid and the controller but am getting a message - Request Failure, Status Code 200, Status Text OK

    The view code is

    Html.X().FileUploadField()
                                        .FieldLabel("Upload File Attachments")
                                        .Name("FileAttachment")
                                        .ID("FileAttachment")
                                        .Padding(5)
            // .TabIndex(30)
                                        .Width(430),
            // text box for file discription HERE
                                    Html.X().TextField()
                                        .FieldLabel("File Discription")
                                        .Name("File Discription")
                                        .ID("FileDiscription")
            //.TabIndex(24)
                                        .Padding(5)
                                        .Width(430),
            // Upload button here
                                Html.X().Button().Text("Upload").Icon(Icon.FolderUp)
                                .DirectEvents (de =>
                                {
                                    de.Click.Url = Url.Action("UploadFile");
                                    de.Click.ExtraParams.Add(new Parameter("discription", "App.FileDiscription.getValue()", ParameterMode.Raw));
                                    de.Click.ExtraParams.Add(new Parameter("id", "App.CounterpartyId.getValue()", ParameterMode.Raw));
                                   // de.Click.Success = "App.FileGrid.store.load()";
                                }
                                )
                            
                            
                            ,
                            // Upload button here
                                Html.X().GridPanel()
                                .ID("FileGrid")
                                .Title("Uploaded Files")
                                .Width(445)
                                .Height(400)
                                .Padding(5)
            //.Frame(true)
                                .Region(Region.West)
                                
                                .Store(Html.X().Store()
                                    .ID("StoreFileStore")
                                    .AutoLoad(false)
                                    .Model(Html.X().Model()
                                        .IDProperty("FileId")
                                            .Fields(
                                            new ModelField("FileId", ModelFieldType.Auto),
                                            new ModelField("FileName", ModelFieldType.String),
                                            new ModelField("ContentType", ModelFieldType.String),
                                            new ModelField("FileData", ModelFieldType.String),
                                            new ModelField("FileSource", ModelFieldType.String),
                                            new ModelField("CounterpartyId", ModelFieldType.Int),
                                            new ModelField("UploadDate", ModelFieldType.Date)
                                            )
                                        )
                                )
                                .View(Html.X().GridView().TrackOver(false))
                                .ColumnModel(
                                    Html.X().Column().Text("File Name").DataIndex("FileName").Width(80),
                                    Html.X().Column().Text("File Description").DataIndex("FileSource").Width(273),
                                    Html.X().Column().Text("Upload Date").DataIndex("UploadDate").Width(80)
                                    )
                                .Features(
                                    Html.X().GridFilters()
                                    .Local(true)
                                    .Filters(
                                        Html.X().StringFilter().DataIndex("FileSource")
                                    )
                                )
                                .DirectEvents(de =>
                                {
                                    de.ItemClick.Url = Url.Action("downloadFile");
                                    de.ItemClick.ExtraParams.Add(new Parameter("FileId", "record.get('FileId')", ParameterMode.Raw));
                                }
                                )
                                )
    And in the controller

                                 public ActionResult UploadFile( string discription, int Id )       
            {
                FileStore fileStore = new FileStore();
                DirectResult result = new DirectResult();
    
                string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
    
                if (this.GetCmp<FileUploadField>("FileAttachment").HasFile)
                {
                    // put the file upload code in here?
                    
                    // Read the file and convert it to Byte Array
                    string filePath = this.GetCmp<FileUploadField>("FileAttachment").PostedFile.FileName;
                    string filename = Path.GetFileName(filePath);
                    string ext = Path.GetExtension(filename);
                    string contenttype = String.Empty;
    
                    //Set the contenttype based on File Extension
                    switch (ext)
                    {
                        case ".doc":
                            contenttype = "application/vnd.ms-word";
                            break;
                        case ".docx":
                            contenttype = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                            break;
                        case ".xls":
                            contenttype = "application/vnd.ms-excel";
                            break;
                        case ".xlsx":
                            contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            break;
                        case ".xlsm":
                            contenttype = "application/vnd.ms-excel.sheet.macroEnabled.12";
                            break;
                        case ".csv":
                            contenttype = "application/vnd.ms-excel";
                            break;
                        case ".pdf":
                            contenttype = "application/pdf";
                            break;
                        default:
                            contenttype = String.Empty;
                            break;
                    }
                    if (contenttype != String.Empty)
                    {
                        Stream fs = this.GetCmp<FileUploadField>("FileAttachment").PostedFile.InputStream;
                        BinaryReader br = new BinaryReader(fs);
                        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        
    
    
                        //insert the file into database
                        fileStore.FileId = Guid.NewGuid();
                        fileStore.FileName = filename;
                        fileStore.ContentType = contenttype;
                        fileStore.FileSource = discription;
                        fileStore.counterpartyId = Convert.ToInt32(Id); 
                        fileStore.FileData = bytes;                            
    
                        fileStore.UploadDate = DateTime.Now;
    
                        // ammend counterparty table to show settlementFileId??
    
                        // add record to database and save changes
                        db.FileStore.Add(fileStore);
                        db.SaveChanges();
                    }
                    else
                    {
                        X.Msg.Show(new MessageBoxConfig
                            {
                                Buttons = MessageBox.Button.OK,
                                Icon = MessageBox.Icon.ERROR,
                                Title = "Fail",
                                Message = "File format not recognised." +
                                  " Upload Image/Word/PDF/Excel formats"
                            }
                            );
                        result.IsUpload = false;
                        return result;
                    }
                    
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO,
                        Title = "Success",
                        Message = string.Format(tpl, this.GetCmp<FileUploadField>("SettlementAttachment").PostedFile.FileName, this.GetCmp<FileUploadField>("SettlementAttachment").PostedFile.ContentLength)
                    });
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = "Fail",
                        Message = "No file uploaded"
                    });
                    result.IsUpload = false;
                    return result;
                }
                
                //// reload the storefilestore
                X.GetCmp<Store>("StoreFileStore").Reload();
                //   // .Reload();
                    
    
    
                result.IsUpload = true;
                return result;
            }   // end of UploadFile
    Last edited by Daniil; Dec 11, 2014 at 11:12 AM. Reason: [CLOSED]
  2. #2
    Hi @OriCoder,

    Please provide a simplified test case to reproduce the problem. Ideally, we should be able to copy, paste and run a test case without any changes from our side.
  3. #3
    It's difficult to provide a full (and concise) sample. Its based on the form example in http://mvc.ext.net/#/Form_FileUploadField/Basic/
    with a grid panel below that shows the saved files. Everything works well except the App.FileGrid.store.load() or X.GetCmp<Store>("StoreFileStore").Reload(); is this what I should be using?
  4. #4
    Hi Daniil,

    I have solved it by calling a new method
    getFileStore(Convert.ToInt32(Id));
    where
    X.GetCmp<Store>("StoreFileStore").Reload();
    was in the controller, the method does this:
     public ActionResult getFileStore(int id) {
                IQueryable FileStore;
    
                FileStore = (from ofg in db.FileStore
                             where ((ofg.counterpartyId == id))
                             select ofg).OrderByDescending(ofg => ofg.UploadDate);
    
    
                X.GetCmp<Store>("StoreFileStore").LoadData(FileStore);
    
                return this.Direct();
            }
    Surely there is an easier way to refresh or reload the grid or store?
  5. #5
    The LoadData method is OK to use in your scenario. I don't think there is an easier way.
  6. #6
    Ok, thanks, you can close this thread off

Similar Threads

  1. Replies: 8
    Last Post: Jun 10, 2013, 1:06 PM
  2. Replies: 1
    Last Post: Mar 15, 2013, 1:38 AM
  3. [CLOSED] Multiple file support to upload in file upload control
    By legaldiscovery in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Feb 01, 2013, 9:41 AM
  4. Replies: 0
    Last Post: Sep 15, 2012, 9:26 AM
  5. Replies: 1
    Last Post: Jun 23, 2011, 9:37 AM

Tags for this Thread

Posting Permissions