1、服务端配置上传文件的大小
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="NMC.HttpService.HttpService" behaviorConfiguration="NmcAuthorization">
<endpoint address="http://127.0.0.1:12375/" binding="webHttpBinding" contract="NMC.HttpService.IHttpService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NmcAuthorization">
<serviceAuthorization serviceAuthorizationManagerType="NMC.Service.NmcServiceAuthorizationManager,NMC.Service">
</serviceAuthorization>
</behavior>
</serviceBehaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="NMC.HttpService.HttpService" behaviorConfiguration="NmcAuthorization">
<endpoint address="http://127.0.0.1:12375/" binding="webHttpBinding" contract="NMC.HttpService.IHttpService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NmcAuthorization">
<serviceAuthorization serviceAuthorizationManagerType="NMC.Service.NmcServiceAuthorizationManager,NMC.Service">
</serviceAuthorization>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</system.serviceModel>
2、web客户端 jquery使用formdata对象,提交时用post方式
//提交上传ftp文件
function postUploadFtpFile() {
var uploadFile = $('#uploadFile')[0].files[0];
if (uploadFile == null) {
alertMsg("请选择上传的文件", "error");
return;
}
var url = "http://" + window.location.host + "/UploadFtpFile?ip=" + gobal_currentNe.IP + "&ftpDir=" + $("#ftpDirPath").text()+"&fileName="+uploadFile.name;
function postUploadFtpFile() {
var uploadFile = $('#uploadFile')[0].files[0];
if (uploadFile == null) {
alertMsg("请选择上传的文件", "error");
return;
}
var url = "http://" + window.location.host + "/UploadFtpFile?ip=" + gobal_currentNe.IP + "&ftpDir=" + $("#ftpDirPath").text()+"&fileName="+uploadFile.name;
var formData = new FormData();
formData.append('file', $('#uploadFile')[0].files[0]);
$.ajax({
url: url,
formData.append('file', $('#uploadFile')[0].files[0]);
$.ajax({
url: url,
type: 'post',
cache: false,
data: formData,
processData: false,
contentType: false,
success: function (jsonstr) {
$('#ftpStatus').text("");
var res = JSON.parse(jsonstr);
if (res == null)
return;
if (res.Result) {
closeModalDialog();
}
else {
alertMsg("上传远程文件【" + uploadFile.name + " 】失败!(" + res.ResultMsg + ")", "error");
}
//刷新当前选中目录
var $selectedDir = $("#ftpTree span.focusRow").first();
var $li = $selectedDir.parent();
getFtpFileList($li);
}
});
}
processData: false,
contentType: false,
success: function (jsonstr) {
$('#ftpStatus').text("");
var res = JSON.parse(jsonstr);
if (res == null)
return;
if (res.Result) {
closeModalDialog();
}
else {
alertMsg("上传远程文件【" + uploadFile.name + " 】失败!(" + res.ResultMsg + ")", "error");
}
//刷新当前选中目录
var $selectedDir = $("#ftpTree span.focusRow").first();
var $li = $selectedDir.parent();
getFtpFileList($li);
}
});
}
3、服务端接口
[WebInvoke(UriTemplate = "UploadFtpFile?ip={ip}&ftpDir={ftpDir}&fileName={fileName}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string UploadFtpFile(Stream formStream,string ip,string ftpDir,string fileName);
string UploadFtpFile(Stream formStream,string ip,string ftpDir,string fileName);
首先要从提交的表单流中提取出文件流,再对文件流做后续处理
//从提交的表单流中获取文件流
public static Stream GetUploadFileStream(Stream formStream)
{
using (MemoryStream ms = new MemoryStream())
{
formStream.CopyTo(ms);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
var position = 0;
//首行
var firstline = sr.ReadLine();
position += firstline.Length + 2;
var line = sr.ReadLine();
while (line != null)
{
position += line.Length + 2;
if (line == "")
break;
line = sr.ReadLine();
}
ms.Position = position;
//截除末行
ms.SetLength(ms.Length - firstline.Length - 6);
var uploadStream = new MemoryStream();
ms.CopyTo(uploadStream);
uploadStream.Position = 0;
return uploadStream;
}
}
上传文件http抓包如下:
-----------------------------7e14f3b207f6
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
test
-----------------------------7e14f3b207f6--
-----------------------------7e14f3b207f6--