向服务器上传文件
//--------------------向服务器上传文件------
//浏览选择需要上传的文件 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
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
[OperationContract]
CustomFileInfo UpLoadFileInfo(CustomFileInfo fileInfo, SysConfig sysConfig);
[OperationContract]
CustomFileInfo GetFileInfo(string fileName);

浙公网安备 33010602011771号