您好,欢迎来到一二三四网。
搜索
您的当前位置:首页.NET实现文件跨服务器上传下载的方法

.NET实现文件跨服务器上传下载的方法

来源:一二三四网


环境说明

两台服务器服务器为A,服务器为B,服务器B为文件服务器

实现方法

1、在A和B上创建用户docshareuser,用户名和密码保持一致

2、B服务器上设置附件文件夹Attachments共享,添加用户docshareuser并设置读写权限

 

3、在A上运行框输入”\\IP\Attachments”,输入用户名密码测试是否共享成功,共享不成功请检查网络及配置问题

4、修改AWeb.config文件附件路径节点的值

<add key="键值" value="\\IP地址\Attachments" />

5、在<system.web>节点下添加如下配置,用户名密码与创建的共享帐号保持一致

<identity impersonate="true" userName="docshareuser" password="密码" />

测试上传成功!下载时报错:

因为下载的方法如下:

context.Response.AppendHeader("Content-Length", fileSize.ToString());
context.Response.CacheControl = HttpCacheability.Public.ToString();
context.Response.Cache.AppendCacheExtension("max-age=" + 365 * 24 * 60 * 60);
context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
context.Response.AppendHeader("ETag", "Never_Modify");
context.Response.Cache.SetETag("Never_Modify");
context.Response.Cache.SetLastModified(DateTime.Now.AddMinutes(-1));
 
context.Response.TransmitFile(filePath);

修改下载方式:

FileStream fs = new FileStream(filePath, FileMode.Open);
 byte[] bytes = new byte[(int)fs.Length];
 fs.Read(bytes, 0, bytes.Length);
 fs.Close();
 Response.ContentType = "application/octet-stream";
 //通知浏览器下载文件而不是打开 
 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
 context.Response.BinaryWrite(bytes);
 context.Response.Flush();
 context.Response.End();

总结

Copyright © 2019- howto1234.net 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务