WCF/C#上传文件

WCF/C#上传文件,c#,wcf,C#,Wcf,我目前正在使用C#和WCF开发一个web服务。我需要做一个上传的方法,所以我下面。现在我可以通过表单上传文件,文件上传到文件夹中,但在浏览器中,总是会返回错误(如在ajax中指定的),所有代码如下: IWebService.cs [ServiceContract] public interface IWebService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fil

我目前正在使用C#和WCF开发一个web服务。我需要做一个上传的方法,所以我下面。现在我可以通过表单上传文件,文件上传到文件夹中,但在浏览器中,总是会返回错误(如在ajax中指定的),所有代码如下:

IWebService.cs

[ServiceContract]
public interface IWebService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
    void UploadFile(string fileName, Stream stream);
}
WebService.svc

public void UploadFile(string fileName, Stream stream)
{
    string FilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Uploads"), fileName);

    int length = 0;
    using (FileStream writer = new FileStream(FilePath, FileMode.Create))
    {
        int readCount;
        var buffer = new byte[8192];
        while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            writer.Write(buffer, 0, readCount);
            length += readCount;
        }
    }
}
TestClient.html

<head>
<script type="text/javascript">
    function UploadFile() {
        fileData = document.getElementById("fileUpload").files[0];
        var data = new FormData();

        $.ajax({
            url: 'http://localhost:1945/Service1.svc/UploadFile?fileName=' + fileData.name,
            type: 'POST',
            data: fileData,
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: "application/octet-stream", // Set content type to false as jQuery will tell the server its a query string request
            success: function (data) {
                alert('successful..');
            },
            error: function (data) {
                alert('Some error Occurred!');
            }
        });
    }

</script>
<title></title>
</head>
<body>
<div>
    <div>
        <input type="file" id="fileUpload" value="" />
        <br />
        <br />
        <button id="btnUpload" onclick="UploadFile()">
            Upload
        </button>
    </div>
</div>
</body>

函数UploadFile(){
fileData=document.getElementById(“fileUpload”).files[0];
var data=new FormData();
$.ajax({
网址:'http://localhost:1945/Service1.svc/UploadFile?fileName='+fileData.name,
键入:“POST”,
数据:fileData,
cache:false,
数据类型:“json”,
processData:false,//不处理文件
contentType:“application/octet stream”//将content type设置为false,因为jQuery将告诉服务器它是一个查询字符串请求
成功:功能(数据){
警报(“成功…”);
},
错误:函数(数据){
警报('发生了一些错误!');
}
});
}


上传
我试图用我的方式解决你的问题

  • 将UploadCustomFile方法的retun类型更改为String并返回一条伪消息

    公共字符串上载自定义文件(字符串文件名,System.IO.Stream) { 字符串FilePath=Path.Combine(HostingEnvironment.MapPath(“~/FileServer/Uploads”),文件名)

  • 通过添加ResponseFormat=WebMessageFormat.Json和字符串返回类型来更改接口方法

    [OperationContract]
    [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
    String UploadCustomFile(string fileName, Stream stream);
    
  • 当我尝试这种方法时,我收到了成功警报消息


    要从服务器端发送自定义异常

    感谢您的评论,这对我很有帮助。您知道我需要为错误添加的返回吗?
    [OperationContract]
    [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
    String UploadCustomFile(string fileName, Stream stream);