javascript_help 0 Newbie Poster

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

<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]

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

//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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.