weixin_33733810 2014-07-22 12:31 采纳率: 0%
浏览 62

将文件发布到WCF服务

I have a WCF service which has a function Upload file.

public void UploadFile(Stream s)
{
    FileStream targetStream = null;
    Stream sourceStream = s;

    string uploadFolder = @"C:\upload\";
    string filePath = Path.Combine(uploadFolder, Guid.NewGuid().ToString());

    using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        //read from the input stream in 6K chunks
        //and save to output stream
        const int bufferLen = 65000;
        byte[] buffer = new byte[bufferLen];
        int count = 0;

        while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
        {
            targetStream.Write(buffer, 0, count);
        }

        targetStream.Close();
        sourceStream.Close();
    }

}


 [ServiceContract]
public interface ITransferService
{
    [OperationContract]
    RemoteFileInfo DownloadFile(DownloadRequest request);

    //[OperationContract]
    //void UploadFile(RemoteFileInfo request);

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/UploadFile")]
    void UploadFile(Stream s);
}

And I am using this Ajax/Jquery:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>AJAX File Upload - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript" src="js/ajaxupload.3.5.js" ></script>
<link rel="stylesheet" type="text/css" href="./styles.css" />
<script type="text/javascript" >
    $(function(){
        var btnUpload=$('#upload');
        var status=$('#status');
        new AjaxUpload(btnUpload, {
            action: 'http://localhost:35711/webservice/TransferService.svc/UploadFile',
            name: 'uploadfile',
            onSubmit: function(file, ext){
                // if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                //    // extension is not allowed 
                //  status.text('Only JPG, PNG or GIF files are allowed');
                //  return false;
                //}
                status.text('Uploading...');
            },
            onComplete: function(file, response){
                //On completion clear the status
                status.text('');
                //Add uploaded file to list
                if(response==="success"){
                    $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
                } else{
                    $('<li></li>').appendTo('#files').text(file).addClass('error');
                }
            }
        });

    });
</script>
</head>
<body>
<div id="mainbody" >
        <h3>&raquo; AJAX File Upload Form Using jQuery</h3>
        <!-- Upload Button, use any id you wish-->
        <div id="upload" ><span>Upload File<span></div><span id="status" ></span>

        <ul id="files" ></ul>
</div>

</body>

The same function works fine when I am using ASP .Net as client. But I am not able to do it with Ajax/Jquery, Is it possible using Ajax/JQuery? and if yes then how?

Can anyone provide a simple JQuery example?

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2014-07-22 16:03
    关注

    For jQuery you need to provider your UploadService as a REST service. In WCF this meens hosting it via webHttpBinding.

    This Binding does not support MessageContracts so you have to adopt your Method signature a little bit.

    [ServiceContract]
    public interface IUploadService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", 
            BodyStyle = WebMessageBodyStyle.Bare, 
            UriTemplate = "Uploadfile?fileName={filename}&length={length}")]
        void UploadFile(string filename, int length, Stream s);
    
    }
    

    Now you have a Url to post your file content to.

    Uploadfile?fileName=Samplefile.jpg&length=72572

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题