web客户端 向 wcf rest服务端 上传文件

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>
    </behaviors>
  </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;
    var formData = new FormData();
    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);
        }
    });
}

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);


首先要从提交的表单流中提取出文件流,再对文件流做后续处理

        //从提交的表单流中获取文件流
        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
test
-----------------------------7e14f3b207f6--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值