I need to update a file which is on a remote server, using Silverlight and C#.
I created the file on the current machine and after that I tried to upload it using this example. It returned no error, but it doesn’t upload my file either.
Could you help me?
this is the ashx code
<%@ WebHandler Language="C#" Class="receiver" %>
using System;
using System.Web;
using System.IO;
public class receiver : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string filename = context.Request.QueryString["DB.xml"].ToString();
using (FileStream fs = File.Create(context.Server.MapPath("~/CLientBin" + 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 {
get {
return false;
}
}
}
and this is the c# code:
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
try
{
UploadFile(dlg.File.Name, dlg.File.OpenRead());
titlu.Text = dlg.File.Name;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
titlu.Text = "No file selected...";
}
}
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://ganduri.elementfx.com/Handler.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);
}
MessageBox.Show("Writed");
}
I have also checked the permissions to the ClientBin folder and are read/write permissions
When I run this code it shows me the “Writed” messageBox, but if I look in the server, the file isn’t anywhere.
I have set a breakpoint in af ashx file functions and id doesn’t reach any of them.
Think you need check upload folder for existence, and write access rights