webservice跨服务器上传附件

时间:2023-03-09 08:45:17
webservice跨服务器上传附件

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

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


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

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

 /// <summary>
/// Summary description for UploadWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class UploadWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
} HttpContext _context = null; [WebMethod]
public string PostFile(Byte[] content, string ext)
{
_context = this.Context;
string text = Convert.ToBase64String(content);
return Upload(ext, text);
} private string Upload(string ext, string content)
{
if (string.IsNullOrEmpty(ext) || string.IsNullOrEmpty(content))
{
return "";
}
//保存图片
string dateNum = DateTime.Now.ToString("yyyyMM");//按月存放
string fileName = Guid.NewGuid() + ext;
string currentPath = HttpContext.Current.Request.PhysicalApplicationPath + "upload\\" + dateNum + "\\";
string fullPath = currentPath + fileName; if (!Directory.Exists(currentPath))
Directory.CreateDirectory(currentPath); byte[] buffer = Convert.FromBase64String(content);
using (FileStream fileStream = new FileStream(fullPath, FileMode.Create))
{
fileStream.Write(buffer, , buffer.Length);
}
string host = _context.Request.Url.Host;
int port = _context.Request.Url.Port;
//ResponseWrite(string.Format("http://" + host + ":" + port + "/upload/{0}/{1}", dateNum, fileName));//返回图片保存路径
return string.Format("http://" + host + "/" + _context.Request.ApplicationPath + "/upload/{0}/{1}", dateNum, fileName);
}
}

简单将其部署到B服务器IIS,访问验证通过,如下:

webservice跨服务器上传附件

步骤二、A服务新建web项目,A服务器的web项目,添加服务引用,增加对B服务器webservice的引用。

新建上传页面,前台页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="school.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" action="WebForm1.aspx" method="post" enctype="multipart/form-data">
<div>
<input type="file" name="upload" id="upload" runat="server" />
<input type="submit" value="upload"/>
</div>
</form>
</body>
</html>

后台代码如下:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var file = Request.Files["upload"];
Stream data = file.InputStream;
int length = (int)data.Length;
byte[] content = new byte[length];
data.Read(content, , content.Length); string ext = file.FileName.Substring(file.FileName.LastIndexOf('.'));
//webservice
var client = new UploadService.UploadWebServiceSoapClient();
string url = client.PostFile(content, ext);
Response.Write(string.Format("<a href=\"{0}\">下载</a>", url));
}
}

步骤三、验证效果,如下图,输出的路径已经是B服务器的网站路径了。

webservice跨服务器上传附件

步骤四、附件大小限制

上传小附件时,没有问题,但是当上传附件很大时,就会报错:

1、没有终结点在侦听可以接受消息的

2、远程服务器返回错误: (404) 未找到
是因为没有设置上传大小的限制,超过了默认值。
在B服务器的webservice的web.config内增加如下配置:
<configuration>
<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" requestLengthDiskThreshold="256" maxRequestLength="2097151" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!--限制附件上传80MB-->
<requestLimits maxAllowedContentLength="81920000" />
</requestFiltering>
</security>
</system.webServer>
</configuration>

重新上传附件,问题解决。


简单思路:分布式服务器应用程序访问同一个上传接口。