I am working on a Silverlight Project. When i saved a jpg picture into a memorystream to save it into the Context.InputStream, it is working fine. I am calling an aspx page who thread the upload into the server.
But i can not do a "response.redirect" or "server.transfer" when the upload is done or failed . Is it because I call my aspx page from Silverlight using WebClient ?
Please find the code in Silverlight below :
private void UploadFile(string fileName, Stream data){
UriBuilder ub = new UriBuilder("http://localhost:52544/WebForm1.aspx");
//add a parameter filename into the queryString
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);
}
On the aspx page, I have this code
protected void Page_Load(object sender, EventArgs e)
{
try
{
// get the filename
string filename = Request.QueryString["filename"].ToString();
// create a file on the server dir
using (FileStream fs = File.Create(Server.MapPath("~/AppData/" + filename)))
{
SaveFile(Request.InputStream, fs);
}
Response.Redirect("uploadOk.aspx", true);
}
catch (Exception ex)
{
}
}
pr开发者_开发百科ivate bool SaveFile(Stream stream, FileStream fs)
{
bool isSaved = true;
byte[] buffer = new byte[4096];
int bytesRead;
try
{
// copy the stream into the file
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
isSaved = true;
}
catch (Exception e)
{
isSaved = false;
}
return isSaved;
}
}
I have tried response.redirection("uploadOk.aspx",false) too and it is not working. I got the following exception “[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}”
Do you have an idea how i can do a redirection using a WebClient ?
Thank you in advance
I believe your problem is that the upload fails but because it is on a different thread, SL does not show the correct error. Try adding code to log the error when it happens and see what the error is. You can do this on the server as well.
I suspect the problem is header is already written so it cannot redirect.
Try this since I think the problem is 100-continue:
c.Headers.Add( HttpRequestHeader.KeepAlive, "false");
c.Headers.Add(HttpRequestHeader.Expect, "");
精彩评论