最近一个项目,用到文件上传功能,本来简单地使用upload控件直接post到服务器保存,简单实现了。后来考虑到分布是部署,静态附件、图片等内容要单独服务器(命名为B服务器,一台,192.168.103.240)存储,则需要分布式服务器(命名为A服务器,可多台,测试程序就是本地 127.0.0.1)上传附件到B服务器。

考虑难易程度和易操作,简单想到的方案是:访问A服务器应用程序调用B服务器的webservice,将附件直接保存到B服务器。


 

简单实验一下,是可以达成效果的。

步骤一、B服务器的webservice代码如下:

 1 /// <summary>
 2     /// Summary description for UploadWebService
 3     /// </summary>
 4     [WebService(Namespace = "http://tempuri.org/")]
 5     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 6     [System.ComponentModel.ToolboxItem(false)]
 7     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 8     // [System.Web.Script.Services.ScriptService]
 9     public class UploadWebService : System.Web.Services.WebService
10     {
11         [WebMethod]
12         public string HelloWorld()
13         {
14             return "Hello World";
15         }
16 
17 
18         HttpContext _context = null;
19 
20         [WebMethod]
21         public string PostFile(Byte[] content, string ext)
22         {
23             _context = this.Context;
24             string text = Convert.ToBase64String(content);
25             return Upload(ext, text);
26         }
27 
28         private string Upload(string ext, string content)
29         {
30             if (string.IsNullOrEmpty(ext) || string.IsNullOrEmpty(content))
31             {
32                 return "";
33             }
34             //保存图片
35             string dateNum = DateTime.Now.ToString("yyyyMM");//按月存放
36             string fileName = Guid.NewGuid() + ext;
37             string currentPath = HttpContext.Current.Request.PhysicalApplicationPath + "upload\\" + dateNum + "\\";
38             string fullPath = currentPath + fileName;
39 
40             if (!Directory.Exists(currentPath))
41                 Directory.CreateDirectory(currentPath);
42 
43             byte[] buffer = Convert.FromBase64String(content);
44             using (FileStream fileStream = new FileStream(fullPath, FileMode.Create))
45             {
46                 fileStream.Write(buffer, 0, buffer.Length);
47             }
48             string host = _context.Request.Url.Host;
49             int port = _context.Request.Url.Port;
50             //ResponseWrite(string.Format("http://" + host + ":" + port + "/upload/{0}/{1}", dateNum, fileName));//返回图片保存路径
51             return string.Format("http://" + host + "/" + _context.Request.ApplicationPath + "/upload/{0}/{1}", dateNum, fileName);
52         }
53 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-08-29
  • 2021-11-28
猜你喜欢
  • 2021-07-12
  • 2021-11-29
  • 2021-05-27
  • 2021-07-16
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案