none
Problem with 'Microsoft.XMLHTTP' in javascript when i try to upload .wmv file to the Silverlight Streaming Service

    Question

  •  

    As per our requirement to upload .wmv file to the Silverlight Streaming Service directly from Client machine. I created a silverlight application that would upload file from client machine to the silverlight streaming server.I did not found Silverlight API that supports me to achieve this task with silverlight. Specially silverlight did not have System.Net.NetworkCredential to pass credentials to the WebClient / HttpWebRequest Object and also both classes not supported Http 'PUT' Method.So that I decided to select file from silverlight using OpenFileDialog, convert file stream into a string and pass it to a javascript function that upload it to the silverlight streaming server using Microsoft.XMLHTTP ActiveX object, because XMLHTTPRequest not supporting Cross Domain.But the problem I am facing is that file is partially uploaded to the silverlight streaming service. It is like to just create a file on server but not transferring complete file.Following is my code can someone help me.

     

     Silverlight application code in Page.Xaml.cs  
    1    private void Upload_Click(object sender, RoutedEventArgs e)
    2            
    3    {
    4                
    5    var dialog = new OpenFileDialog();
    6                
    7    if (dialog.ShowDialog() == true)
    8                
    9    {
    10   var input = dialog.SelectedFile.OpenRead();//getting input stream
    11   byte[] fileBytes = new byte[input.Length];
    12         int byteCount = input.Read(fileBytes, 0, (int)input.Length);
    13                   //Reading Bytes
    14   string fileContent = Encoding.Unicode.GetString(fileBytes, 0, (int)input.Length); //Encoding file content bytes to unicode
    15   
    16   var path = "https://silverlight.services.live.com/71289/DA/" + dialog.SelectedFile.Name;
    17   
    18   object[] param = new object[] 
    19   { path, fileContent, dialog.SelectedFile.Name, dialog.SelectedFile.OpenRead().Length };
    20   HtmlPage.Window.Invoke("sendDataToServer", param); // Calling a javascript method and pass values to upload on SLS Server
    21                   
    22   }
    23   }
    24   
    

    Javascript code into a .js file that is included in .htm file that hosting silverlight plug-in

    1    var httpRequest;
    2    
    3    function CreateObject()
    4    {
    5    
    6    if (typeof ActiveXObject != 'undefined') {
    7    
    8        httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    9    
    10       }
    11   
    12   else 
    13   
    14       if (typeof XMLHttpRequest != 'undefined') {
    15   
    16       httpRequest = new XMLHttpRequest();
    17   
    18       }
    19   
    20       return httpRequest;
    21   
    22       }
    23   
    24   
    25   
    26   function sendDataToServer(url, dataToPost, documentPath, length) {
    27   
    28       CreateObject(); //Creating ActiveX Object
    29       
    30       
    31   if (httpRequest) {
    32   
    33       
    34       httpRequest.open('PUT', url, true,[AccountId],[AccountCode]); // Open http request 
    35   
    36   
    37       httpRequest.onreadystatechange = StatusUpdate; //Assigning Async Event to Check Status 
    38   
    39       httpRequest.setRequestHeader('Content-Type','text/plain');   //Set ContentType to Header 
    40   
    41       httpRequest.setRequestHeader('Content-Length',length); // Set Content Length 
    42   
    43       
    44       httpRequest.send(dataToPost); // Send Data to SLS Server to Upload File
    45   
    46       
    47   return httpRequest;
    48   
    49   }
    50   
    51   else {
    52       return void 0;
    53       }
    54   
    55   }
    56   
    57   
    58   function StatusUpdate() {    
    59   
    60       if (httpRequest.readyState == 0)
    61   
    62           alert("uninitialized – open() has not yet been called");
    63   
    64       else if (httpRequest.readyState == 1) 
    65   
    66           alert("open – send() has not yet been called");
    67   
    68       else if (httpRequest.readyState == 2) 
    69   
    70       {
    71   
    72           alert("sent – send() has been called, headers and status are available");
    73   
    74       }
    75   
    76       else if (httpRequest.readyState == 3) 
    77   
    78           alert("receiving – Downloading, responseText holds partial data");
    79   
    80       else if (httpRequest.readyState == 4) 
    81   
    82       {
    83   
    84               alert("loaded – Finished");
    85   
    86           
    87               alert(httpRequest.status);
    88   
    89               if(httpRequest.status==200)
    90               {
    91               var results = httpRequest.responseXML;
    92   
    93   
    94               }
    95      }
    96   
    

     

    Thanks,

    Faisal

    Saturday, September 06, 2008 5:06 AM