ITPub博客

首页 > Linux操作系统 > Linux操作系统 > winform上传文件暂定方案

winform上传文件暂定方案

原创 Linux操作系统 作者:fangqm 时间:2010-09-30 17:40:05 0 删除 编辑
System.Net.WebClient webclient = new System.Net.WebClient();
            webclient.UploadFile(URL, FileName);

URL 是服务器的URL,就是接受文件的那个页面,FileName是本地文件。所以服务器还必须有个能接受文件上传的页面。JSP与 ASP.NET都可以通用。 
///
        /// WebClient上传文件至服务器
        ///

        /// 文件名,全路径格式
        /// 服务器文件夹路径
        ///
        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;

        }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-675195/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
创业项目 https://u.rscode.cn

注册时间:2010-03-24

  • 博文量
    297
  • 访问量
    784628