首页 > Web开发 > 详细

上传本地文件到服务器

时间:2019-05-18 23:47:37      阅读:148      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspose.Pdf;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            UploadECCInvoice_Intranet("");
        }
        /// <summary>
        /// 内网上传ECC Invoice
        /// </summary>
        /// <param name="path_ECCInvoice">ECCInvoice的路径</param>
        private void UploadECCInvoice_Intranet(string path_ECCInvoice)
        {
            string destinationFile = @"E:\test\DL180226H012.pdf";//要上传的文件
            bool status = false;
            //连接 
            string serverFolder = @"\\192.168.1.188\Evan";
            string PWD = "qwe123!@#";//kingdee1!
            status = connectState(serverFolder, "Administrator", PWD);
            if (status)
            {
                //共享文件夹的目录 
                DirectoryInfo theFolder = new DirectoryInfo(serverFolder);
                string filename = theFolder.ToString();
                //执行方法  
                TransportRemoteToServer(serverFolder + @"\", destinationFile, "test.pdf");    //实现将远程服务器文件写入到本地 
            }
            else
            {
                MessageBox.Show("连接服务器失败!");
            }
        }
        /// <summary> 
        /// 从本地上传文件至服务器
        /// </summary> 
        /// <param name="src">远程服务器路径(共享文件夹路径)</param> 
        /// <param name="dst">本地文件夹路径</param> 
        /// <param name="fileName">上传至服务器上的文件名,包含扩展名</param> 
        public static void TransportRemoteToServer(string src, string dst, string fileName)
        {
            if (!Directory.Exists(dst))
            {
                Directory.CreateDirectory(dst);
            }
            src = src + fileName;
            FileStream inFileStream = new FileStream(src, FileMode.OpenOrCreate);    //从远程服务器下载到本地的文件
            FileStream outFileStream = new FileStream(dst, FileMode.Open);    //远程服务器文件  此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错 
            byte[] buf = new byte[outFileStream.Length];
            int byteCount;
            while ((byteCount = outFileStream.Read(buf, 0, buf.Length)) > 0)
            {
                inFileStream.Write(buf, 0, byteCount);
            }
            inFileStream.Flush();
            inFileStream.Close();
            outFileStream.Flush();
            outFileStream.Close();
        }
        /// <summary> 
        /// 连接远程共享文件夹 
        /// </summary> 
        /// <param name="path">远程共享文件夹的路径</param> 
        /// <param name="userName">用户名</param> 
        /// <param name="passWord">密码</param> 
        /// <returns></returns> 
        public static bool connectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.StandardInput.WriteLine("net use * /del /y");
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    throw new Exception(errormsg);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }
    }
}

上传本地文件到服务器

原文:https://www.cnblogs.com/qiu18359243869/p/10887503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!