简体   繁体   English

如何使用jQuery上传文件?

[英]How to upload file using jquery?

I want to upload a file using jQuery and to store in a folder on the server. 我想使用jQuery上传文件并存储在服务器上的文件夹中。 I don't know how to get started. 我不知道如何开始。 The file path also needs to be stored in an Oracle database. 文件路径也需要存储在Oracle数据库中。 My entire scenario is based on the Entity Framework. 我的整个场景都基于实体框架。 Does anyone have any idea of how to solve this? 有谁知道如何解决这个问题?

Use this plugin for jquery file uploading. 使用此插件上传jquery文件。

http://www.uploadify.com/ http://www.uploadify.com/

OR, use this -- 或者,使用它-

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

OR, Find some more decent plugins at -- 或者,在以下位置找到更多不错的插件-

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

you can upload file easily using iframe in jquery if you don't wanna to use any plugin. 如果您不想使用任何插件,则可以在jquery中使用iframe轻松上传文件。 I was facing that problem from many days now I have resolved that. 从现在开始,我已经解决了很多天了。

$(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>

It will upload file. 它将上传文件。 Now only thing remaining is to receive that file on your server.I have used servlet to get file and after that I called one service. 现在剩下的就是在服务器上接收该文件了。我使用servlet来获取文件,然后我调用了一项服务。 That example will work for every file image,text,docx,doc etc. 该示例适用于每个文件图像,文本,docx,doc等。

Servlet: 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();
}

Enjoy... 请享用...

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

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