上传使用HTML5和/或jQuery的一个文件WCF服务上传、文件、jQuery、WCF

2023-09-10 17:16:10 作者:清醇

我想上传只使用HTML5和JavaScript / jQuery的文件到我的WCF服务。 WCF服务是自行从Windows服务,而不是IIS托管,所以我不认为我可以使用PHP和ASPX的上传处理程序。该文件是二进制,而不是文本。

I am trying to upload a file to my WCF service using only HTML5 and JavaScript/jQuery. The WCF service is self hosted from a Windows service instead of IIS, so I don't think I can use php or aspx upload handlers. The files are binary and not text.

在理想情况下,我想结束了一个.NET流对象,我可以写入文件在本地(服务器端)。在这一点上,我只是在寻找非常非常基本的文件上传。

Ideally, I would like to end up with a .net stream object where I can write the file locally (server side). At this point I'm just looking for very very basic file uploading.

我可以做的JSON通信目前,但不能跃升到二进制文件。目前,我这一步并坚持:

I can do JSON communication currently, but cant make the leap to binary files. Currently I'm this far and stuck:

Service接口:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SendFile")]
string SendFile(Stream stream);

服务实现:

public string SendFile(Stream stream)
        {
            // this works for strings, but i want a FileStream
            StreamReader reader = new StreamReader(stream);
            string data = reader.ReadToEnd();

            return data;
        }

HTML:

 <input type="file" id="fileInput" onchange='sendFile();' />

的JavaScript / jQuery的:

 function sendFile() {
        submitButton.disabled = true; ;
        setInnerText("sending file...");
        var file = document.getElementById('fileInput').files[0];
        var url = "http://10.0.1.9:8732/RainService/SendFile";
        $.ajax({
            url: url,

            type: "POST",
            data: file,
        });
    }

在Chrome的控制台我得到一个非法调用错误。我可以使用数据:file.name ,这会工作得很好但是。我需要发送的文件内容的流这样我就可以在服务器端重建它。

In the Chrome console I get an "Illegal invocation" error. I could use data: file.name and that would work fine however. I need to send a stream of the file contents so i can rebuild it on the server side.

任何想法?

推荐答案

您无法通过AJAX发送文件。 你必须使用一个插件,它具有SWF对象集成,您可以上传一些文件类型(你定义的)。

you cannot send file through ajax. You have to use a plugin which has SWF object integrated where you can upload some file type (you define what).

所以,我建议你使用uploadify(我用它为我的项目MVC3)。

So, I can suggest you to use uploadify (I used it into my project with MVC3).

或者你可以使用: HTTP://www.$c$cproject.com/KB/WCF/WCFDownloadUploadService的.aspx

HTTP://www.$c$cproject.com/KB/ WCF / UploadlargeFilesWCFHTTP.aspx 获得更多的文档

当然,谷歌可以帮助你的更多

Of course, google can help you more