Hi everyone!
I'm trying to upload a file through silverlight.
In visual studio everything work (obviously :( )
This is my code:
private void Upload_Click(object sender, RoutedEventArgs e) { try { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = UPLOAD_DIALOG_FILTER; bool? retVal = dlg.ShowDialog(); if (retVal != null && retVal == true) { progressBar.Visibility = Visibility.Visible; textBox.Visibility = Visibility.Visible; textBox.Text = "Upload starting..."; pathTextBlock.Text = dlg.File.Name; _data = dlg.File.OpenRead(); _bytesTotal = _data.Length; _bytesUploaded = 0; _fileName = dlg.File.Name; progressBar.Maximum = _bytesTotal; UploadFileChunk(); } } catch (Exception ex) { ....... } } private void UploadFileChunk() { try { //UPLOAD_URI= http://localhost:65295/FileUpload.ashx?filename={0}&append={1}&filepath={2} textBox.Text = "Upload in progress..."; string uploadUri = ""; if (_bytesUploaded == 0) { uploadUri = String.Format(UPLOAD_URI, _fileName, 0, FILEPATH); // Dont't append } else if (_bytesUploaded < _bytesTotal) { uploadUri = String.Format(UPLOAD_URI, _fileName, 1, FILEPATH); // append } else { return; // Upload finished } byte[] fileContent = new byte[CHUNK_SIZE]; int bytesRead = _data.Read(fileContent, 0, CHUNK_SIZE); _data.Flush(); WebClient wc = new WebClient(); wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); Uri u = new Uri(uploadUri); wc.OpenWriteAsync(u, null, new object[] { fileContent, bytesRead }); _bytesUploaded += fileContent.Length; } catch (Exception ex) { ....... } } void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { try { progressBar.Value = _bytesUploaded; if (e.Error == null) { object[] objArr = e.UserState as object[]; byte[] fileContent = objArr[0] as byte[]; int bytesRead = Convert.ToInt32(objArr[1]); Stream outputStream = e.Result; outputStream.Write(fileContent, 0, bytesRead); outputStream.Close(); if (_bytesUploaded < _bytesTotal) { UploadFileChunk(); } else { textBox.Text = "Upload complete!"; } } } catch (Exception ex) { ....... } }
Here is my ashx page
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class FileUpload : IHttpHandler { public void ProcessRequest(HttpContext context) { try { if (context.Request.InputStream.Length == 0) throw new ArgumentException("No file input"); if (context.Request.QueryString["fileName"] == null) throw new Exception("Parameter fileName not set!"); if (context.Request.QueryString["filepath"] == null) throw new Exception("Parameter filepath not set!"); string fileName = context.Request.QueryString["fileName"]; string path = context.Request.QueryString["filepath"]; string filePath = Path.Combine(path, fileName); bool appendToFile = context.Request.QueryString["append"] != null && context.Request.QueryString["append"] == "1"; FileMode fileMode; if (!appendToFile) { if (File.Exists(filePath)) File.Delete(filePath); fileMode = FileMode.Create; } else { fileMode = FileMode.Append; } bool uploadSuccesful = false; while (!uploadSuccesful) { try { using (FileStream fs = File.Open(filePath, fileMode)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0) { fs.Write(buffer, 0, bytesRead); } fs.Flush(); fs.Close(); uploadSuccesful = true; } } catch (IOException ex) { throw new Exception("IOException:" + ex.Message); } catch (Exception ex) { throw new Exception("Exception:" + ex.Message); } } } catch (Exception ex) { .......... } } public bool IsReusable { get { return false; } } }
When i debug my project, at the end of the upload i see "Upload complete" and i can find the file in my path.
When i release my project in IIS 7, at the end of the upload i see "Upload complete" but i can't find the file in my path.
I get no error and I don't understand the problem!
Any advice?
Thank you!!!!!
Federica