向服务器上传文件

//--------------------向服务器上传文件------

复制代码
//浏览选择需要上传的文件
  private static bool Unzip(string address, string filezip)
    {
        //创建对话框
         OpenFileDialog ofd = new OpenFileDialog();
         ofd.Title = "请选择上传的文件";
         //规定文件类型
         ofd.Filter = "zip(*.zip)|*.zip;";
         //判断是否选择文件
          if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            //获得文件的完整路径(包括名字后后缀)
            FilePath = ofd.FileName;
            //计算文件KB大小.字节/1024
            txtFileSize.Text = Math.Round(new System.IO.FileInfo(FilePath).Length / 1024.0, 2).ToString();
        }

    }
复制代码

//上传文件

复制代码
private void UploadFileIng()
    {

        //截止文件名称
        string fileName = FilePath.Substring( FilePath.LastIndexOf("\\") + 1);
        //文件上传
        System.IO.FileInfo fileInfoIO = new System.IO.FileInfo(FilePath);
        FileStream fs = File.OpenRead(fileInfoIO.FullName);
        //获取文件MD5值
        try
        {
            int maxSiz = 1024 * 100;
            // 根据文件名获取服务器上的文件
            CustomFileInfo file = client.GetFileInfo(fileInfoIO.Name);
            if (file == null)
            {
                file = new CustomFileInfo();
                file.OffSet = 0;
            }
            file.Name = fileInfoIO.Name;
            file.Length = fs.Length;
            fs.Close();
            fs.Dispose();
            if (file.Length == file.OffSet) //如果文件的长度等于文件的偏移量,说明文件已经上传完成
            {
                MessageBox.Show("该文件已存在");
            }
            else
            {
                //while (file.Length != file.OffSet)
                //{
                file.SendByte = new byte[file.Length - file.OffSet <= maxSiz ? file.Length - file.OffSet : maxSiz]; //设置传递的数据的大小
                file = client.UpLoadFileInfo(file, sysConfig); //上传
                //    //int percent = (int)((double)file.OffSet / (double)((long)file.Length)) * 100;
                //    int percent = (int)(((double)file.OffSet / (double)((long)file.Length)) * 100);
                //}
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            
            client.Close();
            client.Abort();
        }
    }
复制代码

//客户端请求配置ServerClient

public CustomFileInfo UpLoadFileInfo(CustomFileInfo fileInfo,SysConfig sysconfig)
    {
        return this.Channel.UpLoadFileInfo(fileInfo, sysconfig);
    }

//实现接口 Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public CustomFileInfo UpLoadFileInfo(CustomFileInfo fileInfo,SysConfig sysconfig)
    {
        // 获取服务器文件上传路径
        string fileUpLoadPath = System.Web.HttpContext.Current.Server.MapPath(sysconfig.addressFile);
        // 如需指定新的文件夹,需要进行创建操作。
        if (!Directory.Exists(fileUpLoadPath))
        {
            Directory.CreateDirectory(fileUpLoadPath);
        }
        // 创建FileStream对象
        FileStream fs = new FileStream(fileUpLoadPath + fileInfo.Name, FileMode.OpenOrCreate);
        long offSet = fileInfo.OffSet;
        // 使用提供的流创建BinaryWriter对象
        var binaryWriter = new BinaryWriter(fs, Encoding.UTF8);
 
        binaryWriter.Seek((int)offSet, SeekOrigin.Begin);
        binaryWriter.Write(fileInfo.SendByte);
        fileInfo.OffSet = fs.Length;
        fileInfo.SendByte = null;
        fileInfo.path = fileUpLoadPath ;
 
        binaryWriter.Close();
        fs.Close();
        return fileInfo;
    }
    public CustomFileInfo GetFileInfo(string fileName)
    {
 
        string filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/UpLoadFile/") + fileName;
        if (File.Exists(filePath))
        {
            var fs = new FileStream(filePath, FileMode.OpenOrCreate);
            CustomFileInfo fileInfo = new CustomFileInfo
            {
                Name = fileName,
                OffSet = fs.Length,
            };
            fs.Close();
            return fileInfo;
        }
        return null;
    }

  //定有接口 IServer

1
2
3
4
    [OperationContract]
 CustomFileInfo UpLoadFileInfo(CustomFileInfo fileInfo, SysConfig sysConfig);
 [OperationContract]
CustomFileInfo GetFileInfo(string fileName);

  

posted @   一招致命九虎山  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
点击右上角即可分享
微信分享提示