如何使用javascript发送文件 [英] How to send file using javascript

查看:75
本文介绍了如何使用javascript发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个上传页面,需要上传文件。我在这里使用spring框架我的查询是上传按钮我正在调用一个javascript方法,它应该使用jquery ajax将我的文件发送到控制器。有什么办法通过javascrpipt传递这个?以下是我正在尝试的代码。谢谢



i已经粘贴了我的控制器示例,我将String对象发送到控制器,同样有没有办法将文件对象发送到控制器?



I am trying to make an upload page which takes file to upload.I am using spring framework here my query is on Upload button i am calling a javascript method which should send my file to controller using jquery ajax.Is there any way to pass this through javascrpipt ? Following is the code which i am trying. Thanks

i have pasted the example of my controller where i am sending String object to controller,likewise is there any way to send file object to controller ?

<body>
    <div style="text-align: center; margin-removed 60px;">
        <form enctype="multipart/form-data">
            Select file:
            <input type="file" name="dataFile" id="fileAttachment"/><br/><br/>
                <div style="text-align: center; margin-removed 100px;">
                    <input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" />
                </div>
        </form>
    </div>
</body>




 <script language="Javascript">
function uploadAttachment(){
    var Name = jQuery('#Name option:selected').text();
    jQuery.post('upload',{Name:Name}
    function(data){
    if(data==1)
    alert("success");
    else
    alert("failed");
    });
}
    </script>

    on controller.java page following is the code written


@RequestMapping(value = "upload", method=RequestMethod.POST)
        public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
@RequestParam("Name") String Name){
System.out.println(Name);
}

推荐答案

您无法从JavaScript(至少在浏览器中)访问操作系统,并出于安全原因。

它包括文件系统,注册表,驱动程序等......
You can not access OS from JavaScript (in browsers at least), and that for security reasons.
It includes file system, registry, drivers and so on...


这篇关于如何使用javascript发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆