繁体   English   中英

如何使用jQuery上传文件?

[英]How to upload file using jquery?

我想使用jQuery上传文件并存储在服务器上的文件夹中。 我不知道如何开始。 文件路径也需要存储在Oracle数据库中。 我的整个场景都基于实体框架。 有谁知道如何解决这个问题?

使用此插件上传jquery文件。

http://www.uploadify.com/

或者,使用它-

http://blueimp.github.com/jQuery-File-Upload/

或者,在以下位置找到更多不错的插件-

http://www.tutorialchip.com/jquery/9-powerful-jquery-file-upload-plugins/

如果您不想使用任何插件,则可以在jquery中使用iframe轻松上传文件。 从现在开始,我已经解决了很多天了。

$(document).ready(function () {
    $("#formsubmit").click(function () {       

        var iframe = $('<iframe name="postframe" id="postframe" class="hidden" src="about:none" />');         

        $('div#iframe').append(iframe);        

        $('#theuploadform').attr("action", "/ajax/user.asmx/Upload")
        $('#theuploadform').attr("method", "post")
        $('#theuploadform').attr("userfile", $('#userfile').val())
        $('#theuploadform').attr("enctype", "multipart/form-data")
        $('#theuploadform').attr("encoding", "multipart/form-data")
        $('#theuploadform').attr("target", "postframe")         
        $('#theuploadform').submit();
        //need to get contents of the iframe
        $("#postframe").load(
            function () {
                iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
                $("div#textarea").html(iframeContents);
            }
        );



<div id="uploadform">
    <form id="theuploadform" action="">
        <input id="userfile" name="userfile" size="50" type="file" />
        <input id="formsubmit" type="submit" value="Send File" />
    </form>
</div>

<div id="iframe" style="width: 0px; height: 0px; display: none;">
</div>

<div id="textarea">
</div>

它将上传文件。 现在剩下的就是在服务器上接收该文件了。我使用servlet来获取文件,然后我调用了一项服务。 该示例适用于每个文件图像,文本,docx,doc等。

Servlet的:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
    ServletFileUpload upload = new ServletFileUpload();
    response.setContentType("text/plain"); 
    //response.setContentType("application/msword");


    FileItemIterator iterator = upload.getItemIterator(request);

    while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        String filename = item.getName();
        InputStream stream = item.openStream();
        //more here you wanna to do with that file do.
    }
} 
catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

请享用...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM