Dec 11, 2014, 8:26 AM
[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
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]