using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Threading;
using System.IO;
namespace FileInterface
{
[ServiceContract]
public interface ITransfer
{
[OperationContract(Action = "UploadFile")]
void TransferFile(FileTransferMessage request);//文件传输
}
[MessageContract]
public class FileTransferMessage
{
[MessageHeader(MustUnderstand = true)]
public string SavePath;//文件保存路径
[MessageHeader(MustUnderstand = true)]
public string FileName;//文件名称
[MessageBodyMember(Order = 1)]
public Stream FileData;//文件传输时间
}
}
if (_proxy == null)
{
try
{
NetTcpBinding binding = new NetTcpBinding();
binding.TransferMode = TransferMode.Streamed;
binding.SendTimeout = new TimeSpan(0, 30, 0);
//利用通道创建客户端代理
_proxy = ChannelFactory<ITransfer>.CreateChannel(binding, new EndpointAddress(CBSerURL.Text));
IContextChannel obj = _proxy as IContextChannel;
//string s = obj.SessionId;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
public void TransferFile(FileTransferMessage request)
{
string logInfo;
Program.Get_ILog().Log(logInfo = string.Format("开始接收文件,name={0}", request.FileName));//填写日志
//文件信息
string uploadFolder = AppValue.GetParam()._saveDir;
string savaPath = request.SavePath;
string fileName = request.FileName;
Stream sourceStream = request.FileData;
FileStream targetStream = null;
//判断文件是否可读
if (!sourceStream.CanRead)
{
throw new Exception("数据流不可读!");
}
if (savaPath == null) savaPath = @"文件传输\";
if (!savaPath.EndsWith("\\")) savaPath += "\\";
if (!uploadFolder.EndsWith("\\")) uploadFolder += "\\";
uploadFolder = uploadFolder + savaPath;
//创建保存文件夹
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
}
int fileSize = 0;
string filePath = Path.Combine(uploadFolder, fileName);//Combine合并两个路径
try
{
//文件流传输
using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//定义文件缓冲区
const int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
fileSize += count;
}
targetStream.Close();
sourceStream.Close();
}
}
catch (Exception ex)
{
Program.Get_ILog().Log(logInfo + ex.Message);
}
Program.Get_ILog().Log(string.Format("接收文件完毕 name={0},filesize={1}",
request.FileName, fileSize));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有