Upload all the file in a folder to Server using JavaScript / web service

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Mar 2009
Posts: 1
Reputation: javascript_help is an unknown quantity at this point 
Solved Threads: 0
javascript_help javascript_help is offline Offline
Newbie Poster

Upload all the file in a folder to Server using JavaScript / web service

 
0
  #1
Mar 3rd, 2009
Problem: Upload all the file in a folder to Server using JavaScript / web service

I was trying to upload all the files from the specified folder from the client machine to the server.
At the client side, I used JavaScript to read the entire file from client machine
While looping through each file in the folder I call web service and pass the file content. I use webservice.htc to call web service

  1. <html>
  2. <head>
  3. <title>Upload Files</title>
  4. <script language="JavaScript">
  5. var iCallID;
  6. function InitializeService()
  7. {
  8. checkFile();
  9. }
  10.  
  11. function ShowResult()
  12. {
  13. alert(event.result.value);
  14. }
  15. function checkFile()
  16. {
  17.  
  18. var found=0;
  19. var foldobj=new ActiveXObject("Scripting.FileSystemObject");
  20. if((navigator.userAgent.indexOf('Win') != -1))
  21. {
  22. var myfolder=foldobj.GetFolder("c:\\Temp");
  23. var z=myfolder.Files.Count;
  24. var fil_col=myfolder.Files;
  25. var en=new Enumerator(fil_col);
  26. for(;!en.atEnd();en.moveNext())
  27. {
  28.  
  29. var objFileIO = foldobj.GetFile("c:\\Temp\\" + en.item().Name);
  30. var textStreamObject = objFileIO.OpenAsTextStream(1,-2);
  31.  
  32. var strFileContent = textStreamObject.ReadAll();
  33.  
  34. //Another code
  35. var streamIO = objFileIO.OpenAsTextStream();
  36. var strTransform = new Array();
  37. for (i = 0; i < objFileIO.Size; i++) {
  38. var strContent = streamIO.Read(1);
  39. // strTransform[i] = strContent.charCodeAt(0);
  40. strTransform[i] = strContent;
  41.  
  42. }
  43. streamIO.Close();
  44.  
  45.  
  46. var StrPath, StrFile, byteStream;
  47. StrPath= "C:\\Test\\";
  48. StrFile = en.item().Name;
  49.  
  50. service.useService("http://localhost:46967/MyWebService.asmx?wsdl", "UploadDocumentService");
  51. service.UploadDocumentService.callService("UploadDocument",StrPath,StrFile,strFileContent);
  52. //service.UploadDocumentService.callService("UploadDocument",StrPath,StrFile,strTransform);
  53. }
  54. }
  55. return found;
  56. }
  57.  
  58. </script>
  59. </head>
  60. <body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()">
  61. </body>
  62. </html>


At the server, I will write the file back to the specified folder location using FileStream.

[WebMethod]
  1. public string InsertDocument(String strDocPath, String strFile, byte[] document)
  2. {
  3. try
  4. {
  5. string FullPath;
  6.  
  7. FullPath = AppPath + strFile;
  8.  
  9. // *** Write to file ***
  10.  
  11. // Specify file, instructions, and privelegdes
  12. System.IO.FileStream file = new System.IO.FileStream(FullPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Write, document.Length);
  13.  
  14.  
  15. file.Write(document, 0, document.Length);
  16.  
  17. // Close file
  18. file.Close();
  19.  
  20. }
  21. catch (Exception)
  22. {
  23.  
  24. throw;
  25. }
  26. return "Success";
  27. }

Here is the problem it works fine for the text file as I use FileSystemObject and OpenAsTextStream .
But it if tried to upload .doc file, jpeg file etc.(I mean binary file )it fails, the file created with same size, but not able to open these files, says ‘file is corrupted ‘

Can I use
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. //Test another method
  2. var adoStream, stream;
  3. adoStream = new ActiveXObject("ADODB.Stream");
  4. adoStream.Open();
  5. adoStream.Type = 1;//adTypeBinary;
  6. adoStream.LoadFromFile("c:\\Temp\\" + en.item().Name);
  7. stream = adoStream.Read();
  8. adoStream.Close();
  9.  
  10. //End

But I have some issue like will “ADODB.Stream” work on IE 5.1 or later. ?.

Any help is greatly appreciated..
Last edited by peter_budo; Mar 4th, 2009 at 7:50 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC