c# - File upload not triggering handler? -
i'm not sure why, handler isn't getting triggered. (i'm still new silverlight way.) following tutorial here. i'm not sure did wrong... thanks. [using silverlight 3.0, vs2008]
mainpage.xaml
<usercontrol x:class="multiselectfileuploader.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" d:designwidth="640" d:designheight="480"> <grid x:name="layoutroot"> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <textbox x:name="tbul" grid.row="0" width="150" margin="0 0 250 0"></textbox> <button x:name="btnul" grid.row="0" width="100" click="btnul_click"></button> <listbox grid.row="1" width="200" height="25" margin="0 0 200 0"></listbox> <button grid.row="1" width="50" margin="50 0 0 0"></button> <textbox grid.row="2" width="125" margin="0 0 275 0"></textbox> <textbox grid.row="2" width="125" margin="0 0 25 0"></textbox> </grid> </usercontrol>
mainpage.xaml.cs
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using system.io; namespace multiselectfileuploader { public partial class mainpage : usercontrol { public mainpage() { initializecomponent(); btnul.content = "upload"; } protected void btnul_click(object sender, eventargs e) { openfiledialog ofd = new openfiledialog() { multiselect = false }; if ((bool)ofd.showdialog()) // if file selected { uploadfile(ofd.file.name, ofd.file.openread()); } } private void uploadfile(string filename, stream data) { uribuilder ub = new uribuilder("http://localhost:64168/receiver.ashx"); ub.query = string.format("filename={0}", filename); webclient wc = new webclient(); wc.openreadcompleted += (sender, e) => { pushdata(data, e.result); e.result.close(); data.close(); }; wc.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); } } }
receiver.ashx
<%@ webhandler language="c#" codebehind="receiver.ashx.cs" class="multiselectfileuploader.web.receiver" %> using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using system.io; public class receiver : ihttphandler { public void processrequest(httpcontext context) { string filename = context.request.querystring["filename"].tostring(); using (filestream fs = file.create(context.server.mappath("~/temp/" + filename))) { savefile(context.request.inputstream, fs); } } private void savefile(stream stream, filestream fs) { byte[] buffer = new byte[4096]; int bytesread; while ((bytesread = stream.read(buffer, 0, buffer.length)) != 0) fs.write(buffer, 0, bytesread); } public bool isreusable { { return false; } } }
i'm dumb...
the line... wc.openreadcompleted += (sender, e) =>
should be... wc.openwritecompleted += (sender, e) =>
Comments
Post a Comment