Skip to main content

Microsoft Silverlight

Upload File on ServerRSS Feed

(0)

kalimevole
kalimevole

Member

Member

11 points

28 Posts

Upload File on Server

Hi,

I've tried  implement uploading files on server along this tutorial http://silverlight.net/learn/learnvideo.aspx?video=69793 by Tim Heuer. But i have a problem because it does not work.

Here is the code:

 Page.xaml.cs:

 

1    public void mujButton_Click(object sender, RoutedEventArgs e)
2            {
3                OpenFileDialog dlg = new OpenFileDialog();
4                dlg.Multiselect = false;
5                dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
6    
7                if ((bool)dlg.ShowDialog()) // open/uploaded
8                {
9                    UploadFile(dlg.File.Name, dlg.File.OpenRead());
10                   tb_jmenoSouboru.Text = dlg.File.Name.ToString();
11               }
12               else
13               {
14                   tb_jmenoSouboru.Text = "nebyl vybran zadny soubor...";
15               }
16   
17           }
18   
19           private void UploadFile(string filename, Stream data)
20           {
21               UriBuilder ub = new UriBuilder("http://localhost:1734/reciever.ashx");
22               ub.Query = string.Format("filename={0}", filename);
23   
24               WebClient c = new WebClient();
25               c.OpenWriteCompleted += (sender, e) =>
26                   {
27                       PushData(data, e.Result);
28                       e.Result.Close();
29                       data.Close();
30                   };
31               c.OpenWriteAsync(ub.Uri);
32           }
33   
34           private void PushData(Stream input, Stream output)
35           {
36               byte[] buffer = new byte[4096];
37               int bytesRead;
38   
39               while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
40               {
41                   output.Write(buffer, 0, bytesRead);
42               }
43           }
 
and reciever.ashx:
 
1    public class reciever : IHttpHandler
2        {
3    
4            public void ProcessRequest(HttpContext context)
5            {
6                string filename = context.Request.QueryString["filename"].ToString();
7    
8                using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename)))
9                {
10                   SaveFile(context.Request.InputStream, fs);
11               }
12           }
13   
14           private void SaveFile(Stream stream, FileStream fs)
15           {
16               byte[] buffer = new byte[4096];
17               int bytesRead;
18               while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
19               {
20                   fs.Write(buffer, 0, bytesRead);
21               }
22           }
23   
24           public bool IsReusable
25           {
26               get
27               {
28                   return false;
29               }
30           }
  

  I realy do not know where the problem is. Does anybody know where the problem?

Thx for answers

Kali

struggle-luan
struggle...

Member

Member

578 points

102 Posts

Re: Upload File on Server

I don't know Exactly, but I've tried as same method as you to do Upload.

There're some Problems you may have MET.

1) Web Services have limited Max Data Size, you may have to try to Upload a file which is about 5/6 bytes large. see if it works. (To change the limitation, pls modify Web.Config)

2) Try to add "fs.Flush();"  next to your "fs.Write().."

3) Add break poiints to "ProcessRequest" and see if your Webservice accessed the Request.

4) Sometimes the Openwrite method dosen't work, and try "UploadString", if it can be catched by break point, there's no idea... to solve.

And finally, hope you can fix your problem, and try to use WCF.... That will be an easy way to show the Progress of Uploading process..

Hope those helps

ehh... this is what I called "POOR ENGLISH"..

prujohn
prujohn

Contributor

Contributor

3569 points

704 Posts

Re: Upload File on Server

You have cross domain policies in place, correct?

kalimevole
kalimevole

Member

Member

11 points

28 Posts

Re: Upload File on Server

 Maybe i definated a problem closer - if I add break points to ProcessRequest, I see the method is not called. Maybe it is a problem with a calling reciever.ashx? But still do not know how to solve it.

struggle-luan
struggle...

Member

Member

578 points

102 Posts

Re: Upload File on Server

ASMX is not called by client. You have to check your Remote URI.

ehh... this is what I called "POOR ENGLISH"..
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities