i have download code openfiledialog (file upload function) silverlight tutorials website. creating silverlight application using esri api , incorporate file upload functionality in it. have replicated exact code in application there no errors when run it, reason application dosen't execute line of code "c.openwriteasync(ub.uri)"
edit 2: notice thing when upgraded general handler (receiver.ashx) downloaded has following first line while generic handler doesn't
<%@ webhandler language="c#" class="receiver" %> dont know why code dosent trigger :(
here code
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.browser; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.markup; using system.windows.shapes; using system.componentmodel; using esri.arcgis.client; using system.windows.controls.primitives; using system.io; using system.io.isolatedstorage; namespace datatool { public partial class mainpage : usercontrol { public mainpage() { initializecomponent(); loaded += new routedeventhandler(mainpage_loaded); } void mainpage_loaded(object sender, routedeventargs e) { // htmlpage.registerscriptableobject("silverlightlearn", this); } [scriptablemember] private void btnservice_click(object sender, routedeventargs e) { } private void btnupload_click(object sender, routedeventargs e) { openfiledialog dialog = new openfiledialog(); dialog.multiselect = false; dialog.filter = "all files|*.*"; bool? selfil = dialog.showdialog(); if (selfil != null && selfil == true) { string selectedfilename = dialog.file.name; uploadfile(selectedfilename, dialog.file.openread()); } else { //do else } } private void storeiso(string filename, stream data) { } private void uploadfile(string filename, system.io.stream data) { // webclient wbc = new webclient(); uribuilder ub = new uribuilder("http://localhost:63461/datatool/datareceiver.ashx"); ub.query = string.format("filename={0}", filename); webclient c = new webclient(); c.openwritecompleted += (sender, e) => { pushdata(data, e.result); e.result.close(); data.close(); }; c.openwriteasync(ub.uri); } private void pushdata(stream input, stream output) { byte[] buffer = new byte[4096]; int bytesread; while ((bytesread = input.read(buffer, 0, buffer.length)) != 0) { output.write(buffer, 0, bytesread); } } } }
here datareceiver.ashx code
using system; using system.collections.generic; using system.linq; using system.web; using system.io; namespace datatool.web { /// <summary> /// summary description datareceiver /// </summary> public class datareceiver : ihttphandler { public void processrequest(httpcontext context) { string filename = context.request.querystring["filename"].tostring(); using (filestream fs = file.create(context.server.mappath("~/app_data/" + filename))) { savefile(context.request.inputstream, fs); } } public void savefile(stream st, filestream fs) { byte[] buffer = new byte[4096]; int bytesread; while ((bytesread = st.read(buffer, 0, buffer.length)) != 0) { fs.write(buffer, 0, bytesread); } } public bool isreusable { { return false; } } } }
i have gone through downloaded sample code , code step step , found code dosent execute openwriteasync statement. downloaded code in .net 3.5 or 3.0 framework , upgraded 4.0.
edit: please find sample here https://rapidshare.com/files/459667631/testing.zip
this simple , check following code
openfiledialog dlg = new openfiledialog(); dlg.filter = "text files (*.txt)|*.txt"; if (dlg.showdialog() == dialogresult.ok) { using (streamreader reader = dlg.selectedfile.opentext()) // store file content in 'text' variable string text = reader.readtoend(); } } c# example 2: copy files application's isolated storage. using system.windows.controls; using system.io; using system.io.isolatedstorage; ... openfiledialog dlg = new openfiledialog(); dlg.filter = "all files (*.*)|*.*"; dlg.enablemultipleselection = true; if (dlg.showdialog() == dialogresult.ok) { // save selected files application's isolated storage isolatedstoragefile iso = isolatedstoragefile.getuserstoreforapplication(); foreach (filedialogfileinfo file in dlg.selectedfiles) { using (stream filestream = file.openread()) { using (isolatedstoragefilestream isostream = new isolatedstoragefilestream(file.name, filemode.create, iso)) { // read , write data block block until finish while(true) { byte[] buffer = new byte[100001]; int count = filestream.read(buffer, 0, buffer.length); if (count > 0) { isostream.write(buffer, 0, count); } else { break; } } } } } }
Comments
Post a Comment