zoukankan      html  css  js  c++  java
  • WinForm上传文件至服务器

            /// <summary>
            /// WebClient上传文件至服务器
            /// </summary>
            /// <param name="localFilePath">文件名,全路径格式</param>
            /// <param name="serverFolder">服务器文件夹路径</param>
            /// <returns></returns>
            public bool Upload(string localFilePath, out string folderName,string newFileName)
            {
                //先创建文件夹
                folderName = "";
                try
                {
                    Guid guid = Guid.NewGuid();
                    folderName = guid.ToString();

                    string diskPath = DAL.DataBaseOperator.GetValueFromApplictionConfig("diskPath");
                    if (!diskPath.EndsWith("/") && !diskPath.EndsWith(@"\"))
                    {
                        diskPath = diskPath + "/";
                    }
                    diskPath += folderName;
                    if (!Directory.Exists(diskPath))
                    {

            //服务器创建文件夹
                        Directory.CreateDirectory(diskPath);
                    }
                    //再上传数据
                    string serverFolder = DAL.DataBaseOperator.GetValueFromApplictionConfig("uploadPath");
                    if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith(@"\"))
                    {
                        serverFolder = serverFolder + "/";
                    }
                    string uriString = serverFolder + folderName + "/" + newFileName;

                    /// 创建WebClient实例
                    WebClient myWebClient = new WebClient();
                    myWebClient.Credentials = CredentialCache.DefaultCredentials;

                    // 要上传的文件
                    FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
                    //判断文件大小
                    string strFileSize = DAL.DataBaseOperator.GetValueFromApplictionConfig("fileSize");
                    int fileSize = Convert.ToInt32(strFileSize) * 1024 * 1024;
                    if (fs.Length > fileSize)
                    {
                        MessageBox.Show("您上传的附件不能超过 " + strFileSize + "M");
                        return false;
                    }
                    BinaryReader r = new BinaryReader(fs);
              
                    //使用UploadFile方法可以用下面的格式
                    myWebClient.UploadFile(uriString,"PUT",localFilePath);
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                   
                    if (postStream.CanWrite)
                    {
                        postStream.Write(postArray, 0, postArray.Length);
                    }
                    else
                    {
                        MessageBox.Show("文件目前不可写!");
                    }
                    Application.DoEvents();
                    postStream.Close();
                }
                catch(Exception err)
                {
                    //MessageBox.Show("文件上传失败,请稍候重试~");
                    DAL.Log.FileLogSys.FileLog.WriteLog(err.Message + err.StackTrace);
                    return false;
                }

                return true;

            }

  • 相关阅读:
    bash帮助文档简单学习;bash手册翻译
    jmeter的master、slave模型启动方法
    如何查看pip安装包的所有版本;以及ipython的安装
    简单过下bash/sh脚本基本知识吧
    Celery 启动报错 can_read() got an unexpected keyword argument timeout
    paramiko执行命令超时的问题
    远程的jmeter自动执行完,如何回调通知被调用者“结束”状态
    记录一下:关于mysql数据误删除恢复的问题
    数据库Sharding的基本思想和切分策略
    数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示
  • 原文地址:https://www.cnblogs.com/threestone/p/1714788.html
Copyright © 2011-2022 走看看