| | |
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
![]() |
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
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
At the server, I will write the file back to the specified folder location using FileStream.
[WebMethod]
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
But I have some issue like will “ADODB.Stream” work on IE 5.1 or later. ?.
Any help is greatly appreciated..
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
html Syntax (Toggle Plain Text)
<html> <head> <title>Upload Files</title> <script language="JavaScript"> var iCallID; function InitializeService() { checkFile(); } function ShowResult() { alert(event.result.value); } function checkFile() { var found=0; var foldobj=new ActiveXObject("Scripting.FileSystemObject"); if((navigator.userAgent.indexOf('Win') != -1)) { var myfolder=foldobj.GetFolder("c:\\Temp"); var z=myfolder.Files.Count; var fil_col=myfolder.Files; var en=new Enumerator(fil_col); for(;!en.atEnd();en.moveNext()) { var objFileIO = foldobj.GetFile("c:\\Temp\\" + en.item().Name); var textStreamObject = objFileIO.OpenAsTextStream(1,-2); var strFileContent = textStreamObject.ReadAll(); //Another code var streamIO = objFileIO.OpenAsTextStream(); var strTransform = new Array(); for (i = 0; i < objFileIO.Size; i++) { var strContent = streamIO.Read(1); // strTransform[i] = strContent.charCodeAt(0); strTransform[i] = strContent; } streamIO.Close(); var StrPath, StrFile, byteStream; StrPath= "C:\\Test\\"; StrFile = en.item().Name; service.useService("http://localhost:46967/MyWebService.asmx?wsdl", "UploadDocumentService"); service.UploadDocumentService.callService("UploadDocument",StrPath,StrFile,strFileContent); //service.UploadDocumentService.callService("UploadDocument",StrPath,StrFile,strTransform); } } return found; } </script> </head> <body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()"> </body> </html>
At the server, I will write the file back to the specified folder location using FileStream.
[WebMethod]
Java Syntax (Toggle Plain Text)
public string InsertDocument(String strDocPath, String strFile, byte[] document) { try { string FullPath; FullPath = AppPath + strFile; // *** Write to file *** // Specify file, instructions, and privelegdes System.IO.FileStream file = new System.IO.FileStream(FullPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Write, document.Length); file.Write(document, 0, document.Length); // Close file file.Close(); } catch (Exception) { throw; } return "Success"; }
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)
//Test another method var adoStream, stream; adoStream = new ActiveXObject("ADODB.Stream"); adoStream.Open(); adoStream.Type = 1;//adTypeBinary; adoStream.LoadFromFile("c:\\Temp\\" + en.item().Name); stream = adoStream.Read(); adoStream.Close(); //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.
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Hey crunchie, RE: LOP problem (Viruses, Spyware and other Nasties)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Centering Objects in Iframe
- Next Thread: Auto Refresh
| Thread Tools | Search this Thread |
ajax ajaxexample ajaxjspservlets array browser bug captcha captchaformproblem cart checkbox child class close codes cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps marquee masterpage math matrixcaptcha media menu object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php position post programming progressbar prototype rated redirect runtime safari scale scriptlets scroll search security session shopping size software star stars synchronous toggle unicode variables web webservice wysiwyg \n





