跨服务器上传文件
在。net中怎么跨服务器上传文件,网上搜了些方法,都不能链接成功,还有通过webservice实现的,不知道是不是我什么步骤没注意到,反正就是没实现,我是在局域网中,同时也是域环境,希望各位牛人指点指点。我所有的分数就42分了 --------------------编程问答-------------------- A:上传页面,将上传的文件转换成Byte[],发送给B
ImagesUploadSrv srv = new ImagesUploadSrv();
srv.Url = " ";//B服务地址
HttpFileCollection files = Request.Files;
byte[] b = new byte[files[0].ContentLength];
System.IO.Stream fs = (System.IO.Stream)files[0].InputStream;
fs.Read(b, 0, files[0].ContentLength);
str = srv.ImagesUpload(b, extension);
fs.Close();
B:数据接收Web服务,接收byte[]数据。
[WebMethod]
public string ImagesUpload(byte[] fs, string extension)
{
using (MemoryStream m = new MemoryStream(fs))
using (FileStream f = new FileStream(file, FileMode.Create))
{
m.WriteTo(f);
m.Close();
f.Close();
m.Dispose();
f.Dispose();
}
}
--------------------编程问答-------------------- 利用webservice是可以实现的。
1.先写好web服务类
[WebMethod(Description = "检验文件是否存在,并上传文件到指定路径 返回值(-2文件已存在,-1写文件异常,1上传成功)")]
public int NotExistsUploadFile(byte[] fs, string fileName, string parRoot)
{
int result = 0;
if (parRoot != "")//有路劲
{
string _path = Server.MapPath(parRoot);
//检验文件夹是否存在
if (!Directory.Exists(_path))
{
//创建文件夹,并上传该文件
Directory.CreateDirectory(_path);
string strFile = _path + "\\" + fileName;
try
{
System.IO.MemoryStream m = new System.IO.MemoryStream(fs);
System.IO.FileStream fl = new System.IO.FileStream(strFile, System.IO.FileMode.OpenOrCreate);
m.WriteTo(fl);
m.Close();
fl.Close();
m = null;
fl = null;
result = 1;//写文件成功
}
catch
{
result = -1;//写文件 异常
}
}
else
{
//查询该文件名 是否已经存在了
string strFile = _path + "\\" + fileName;
if (!File.Exists(strFile))
{
try
{
System.IO.MemoryStream m = new System.IO.MemoryStream(fs);
System.IO.FileStream fl = new System.IO.FileStream(strFile, System.IO.FileMode.OpenOrCreate);
m.WriteTo(fl);
m.Close();
fl.Close();
m = null;
fl = null;
result = 1;//写文件成功
}
catch
{
result = -1;//写文件 异常
}
}
else
{
result = -2;//文件已经存在了
}
}
}
return result;
}
2.利用.net自带工具 获取客户端代理类
如 2.WSDL http://192.168.0.37/WebServiceUploadFile .asmx /n:WebSer/out:WebServiceUploadFile .cs
3.把代理类复制到 项目下
利用代理上传
/**/
////获得上传文件名称
//FileInfo file = new FileInfo(fileUpload.PostedFile.FileName);
//将上传文件转换为二进制流
byte[] fileContent = fileUpload.FileBytes;
//获得上传文件名称
string fileName = fileUpload.FileName;
//实例化webservice
WebServiceUploadFile adTemp = new WebServiceUploadFile() ;
int result = adTemp.NotExistsUploadFile(fileContent, fileName, "server");
Response.Write(result);
补充:.NET技术 , ASP.NET