PDA

View Full Version : [CLOSED] Refresh grid/store on file upload



OriCoder
Dec 11, 2014, 9:26 AM
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

Daniil
Dec 11, 2014, 10:44 AM
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.

OriCoder
Dec 11, 2014, 11:46 AM
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?

OriCoder
Dec 11, 2014, 11:54 AM
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?

Daniil
Dec 11, 2014, 12:02 PM
The LoadData method is OK to use in your scenario. I don't think there is an easier way.

OriCoder
Dec 11, 2014, 12:10 PM
Ok, thanks, you can close this thread off