예제 #1
0
파일: Form1.cs 프로젝트: ParallaxIT/tdb-ws
        /// <summary>
        /// Upload any file to the web service; this function may be
        /// used in any application where it is necessary to upload
        /// a file through a web service
        /// </summary>
        /// <param name="filename">Pass the file path to upload</param>
        private void UploadFile(string filename)
        {
            try
            {
                // get the exact file name from the path
                String strFile = Path.GetFileName(filename);

                // create an instance fo the web service
                var srv = new TDBWebService.CalUploader();

                // create an instance fo the web service
                var srvMain = new TDBMainWebService.tdb_wsv_auth();
                //string verHash = srvMain.ValidateLoggedInUser("robs", "Enigma!");
                string verHash = srvMain.ValidateLoggedInUser("computerrivet", "HDd8fZ6hwUq8ls");

                if (verHash == "ERRORNOTFOUND")
                {
                    MessageBox.Show("User not authenticated", "Authentication Error");
                    return;
                }
                if (verHash == "ERROR")
                {
                    MessageBox.Show("Error in user authenticated", "Authentication Error");
                    return;
                }

                // get the file information form the selected file
                var fInfo = new FileInfo(filename);

                // get the length of the file to see if it is possible
                // to upload it (with the standard 4 MB limit)
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4)
                {
                    // set up a file stream and binary reader for the
                    // selected file
                    var fStream = new FileStream(filename,
                    FileMode.Open, FileAccess.Read);
                    var br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    string sTmp = srv.UploadFileWithVerify(data, strFile, "TeamAsshole", "ASDF1234", verHash);

                    fStream.Close();
                    fStream.Dispose();

                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
                }
                else
                {
                    // Display message if the file was too large to upload
                    MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
                }
            }
            catch (Exception ex)
            {
                // display an error message to the user
                MessageBox.Show(ex.Message, "Upload Error");
            }
        }
예제 #2
0
파일: Form1.cs 프로젝트: ParallaxIT/tdb-ws
        private void UploadPDFFile(string fileName)
        {
            // get the exact file name from the path
            String strFile = Path.GetFileName(fileName);

            // create an instance fo the web service
            var srv = new TDBWebService.CalUploader();

            // create an instance fo the web service
            var srvMain = new TDBMainWebService.tdb_wsv_auth();
            string verHash = srvMain.ValidateLoggedInUser("computerrivet", "HDd8fZ6hwUq8ls");

            if (verHash == "ERRORNOTFOUND")
            {
                MessageBox.Show("User not authenticated", "Authentication Error");
                return;
            }
            if (verHash == "ERROR")
            {
                MessageBox.Show("Error in user authenticated", "Authentication Error");
                return;
            }

            using (var reader = new StreamReader(fileName))
            {
                var html = reader.ReadToEnd();
                var bytes = srv.CreatePDFFromHTMLFile(html, "MyTitle");

                if (html.Length > 0 && bytes == null)
                {
                    MessageBox.Show("Error Occurred!..PDF Byte array was null");
                }

                // write the bytes to disk
                using (var binaryWriter = new BinaryWriter(new FileStream("output.pdf", FileMode.CreateNew)))
                {
                    binaryWriter.Write(bytes);
                }
            }
        }