Monday, 11 June 2012

Upload file from windows application to web application server


1. First upload a file
2. Upload a file on web server using web services . Call web services in window application .

private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialogXML.Title = "Open File";
            openFileDialogXML.Filter = "All Files|*.*";
            openFileDialogXML.FileName = "";

            try
            {
                openFileDialogXML.InitialDirectory = "C:\\Temp";
            }
            catch (Exception ex)
            {
                throw ex;
            }

            openFileDialogXML.ShowDialog();

            if (openFileDialogXML.FileName == "")
                return;
            else
                txtFileName.Text = openFileDialogXML.FileName;

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if (txtFileName.Text != string.Empty)
                UploadFile(txtFileName.Text);
            else
                MessageBox.Show("You must select a file first.", "No File Selected");
        }

        private void UploadFile(string filename)
        {
            try
            {

                String strFile = System.IO.Path.GetFileName(filename);
                FileUploader.Uploader srv = new FileUploader.Uploader();
                FileInfo fInfo = new FileInfo(filename);
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);
                if (dLen < 4)
                {
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();
                    string sTmp = srv.UploadXMLFile(data, strFile);
                    fStream.Close();
                    fStream.Dispose();
                    MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
                }
                else
                {
                    MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Upload Error");
            }
        }



/********************************************************************

web services :>>>>>>>>>>>>


[WebMethod]
        public string UploadXMLFile(byte[] f, string fileName)
        {

            try
            {
                MemoryStream ms = new MemoryStream(f);
                FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/UploadedFile/") + fileName, FileMode.Create);
                ms.WriteTo(fs);
                ms.Close();
                fs.Close();
                fs.Dispose();


                string appPath = HttpRuntime.AppDomainAppPath + "UploadedFile\\" + fileName;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(appPath);
                XMLDocReadVat24XMLFile(xmlDoc);
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }










No comments:

Post a Comment