i'm tring use json file database static content in asp.net (an image gallery). works well: can read file , retrieve data , display them in easy way. i'm using json.net , c# serverside.
this part of json (hand made):
{ "category 1": [ { "titlefile": "title of img1", "filename": "img1", "urlthumbfile": "content/images/gallery/img1_tb.png", "urlfile": "content/images/gallery/img1.png", "descfile": "desc img1" }, { "titlefile": "title of img2", "filename": "img2", "urlthumbfile": "content/images/gallery/img2_tb.png", "urlfile": "content/images/gallery/img2.png", "descfile": "desc img2" } ], "category 2": [ { "titlefile": "title of img3", "filename": "img3", "urlthumbfile": "content/images/gallery/img3_tb.png", "urlfile": "content/images/gallery/img3.png", "descfile": "desc img3" }, { "titlefile": "title of img4", "filename": "img4", "urlthumbfile": "content/images/gallery/img4_tb.png", "urlfile": "content/images/gallery/img4.png", "descfile": "desc img4" } ], "category 3": [ { "titlefile": "title of img5", "filename": "img5", "urlthumbfile": "content/images/gallery/img5_tb.png", "urlfile": "content/images/gallery/img5.png", "descfile": "desc img5" } ] } now, figuring how create sort of "upload form", in order write json file , store infos. client side, ok:
<asp:textbox runat="server" id="txtfilename" /> <asp:textbox runat="server" id="txtfiletitle" /> <asp:textbox runat="server" id="txtfiledesc" /> <asp:fileupload runat="server" id="uplfileimg" /> <asp:dropdownlist runat="server" id="ddlcategory"/> <asp:button runat="server" id="btnuploadfile" text="upload" onclick="btnuploadfile_click" /> c# side:
//classes public class jsongallerycategory //is correct?? { public list<jsongallerycontent> category { get; set; } } public class jsongallerycontent //?? { public string filetitle { get; set; } public string filename { get; set; } public string urlthumbfile { get; set; } public string urlfile { get; set; } public string descfile { get; set; } public datetime datafile { get; set; } } //in .aspx.cs private void populateddl() { list<string> items = new list<string>(); dynamic jsonfile = jobject.parse(utilities.readfile("~/content/images/gallery/gallery.json")); foreach (jproperty category in jsonfile) { items.add(category.name); } ddlcategory.datasource = items; ddlcategory.databind(); } protected void btnuploadfile_click(object sender, eventargs e) { if (uplfileimg.hasfile) { string filename = path.combine(server.mappath("~/content/images/gallery/"), uplfileimg.filename); uplfileimg.saveas(filename); //open jsonfile...read content...deserialize? dynamic jsonfile = jobject.parse(utilities.readfile("~/content/images/gallery/gallery.json")); //create new node? //??? //append new node? //serialize again , save...? ordering nodes?? utilities.writefile("~/content/images/gallery/gallery.json", jsonconvert.serializeobject("???", formatting.indented)); utilities.showmessage("success"); } else { utilities.showmessage("error"); } } how can obtain it?
thanks in advance!
dynamic jsonfile = jobject.parse(utilities.readfile("~/content/images/gallery/gallery.json")); using (filestream fs = file.open(@"~/content/images/gallery/gallery.json", filemode.open)) //may have server.mappath using (streamwriter sw = new streamwriter(fs)) using (jsonwriter jw = new jsontextwriter(sw)) { jw.formatting = formatting.indented; jsonserializer serializer = new jsonserializer(); serializer.serialize(jw, jsonfile); }
Comments
Post a Comment