简体   繁体   English

如何使用提交上传文件而无需重新加载页面

[英]How to upload files using submit without reloading the page

I am uploading files to the tomcat server using a submit button but my problem is that the page is reloading. 我正在使用“提交”按钮将文件上传到tomcat服务器,但是我的问题是页面正在重新加载。 If I use return false the file is not uploaded. 如果我使用return false ,则不会上传文件。 I tried to use button its not uploading the files. 我试图使用按钮不上传文件。

I am using jstl and javascript for my client side. 我在客户端使用jstl和javascript。

<form:form modelAttribute="uploadItem" name="frm" method="post"
    enctype="multipart/form-data">
    <fieldset>
        <legend>Upload File</legend>
        <table>
            <tr>
                <td><form:label for="fileData" path="fileData">File</form:label><br />
                </td>
                <td><form:input path="fileData" id="csv" type="file" /></td>
            </tr>
            <tr>
                <td><br /></td>
                <td><input id="subUpload" type="submit" value="Upload" /></td>
            </tr>
        </table>
    </fieldset>
</form:form>

For my javascript: 对于我的JavaScript:

$("#subUpload").click(function(e) {
    var image = document.getElementById("csv").value;
    if(image!=''){
        var checkimg = image.toLowerCase();
            if (!checkimg.match(/(\.CSV|\.csv)$/)){
                alert("Please enter  Image  File Extensions .jpg,.png,.jpeg");
                document.getElementById("image").focus();
                return false;
            }
    }
    return true;
    e.preventDefault();
});

I also used e.preventDefault() but still its reloading. 我还使用了e.preventDefault()但仍在重新加载它。

Please help. 请帮忙。

如果您使用iframe,这是可能的。

  • Anything under and in the same block with return [optional value here]; 在同一块之内和之下的任何东西都带有return [optional value here]; will be omitted so your e.preventDefault() line will never get executed. 将被省略,因此您的e.preventDefault()行将永远不会执行。
  • return true will tell the button to behave normal, so it will try to take its regular action return true将告诉按钮行为正常,因此它将尝试采取常规操作
  • return false and e.preventDefault() will do the same thing (stop the form from submitting the file), except, return false additionally blocks bubbling the click event up the document. return falsee.preventDefault()会做相同的事情(阻止表单提交文件),不同的是, return false另外会阻止将click事件冒泡到文档中。

If you want to send the file seamlessly you might want to use an iframe or send the data using jQuery instead of letting the form to handle that data transfer. 如果要无缝发送文件,则可能要使用iframe或使用jQuery发送数据,而不是让表单处理该数据传输。

您可以使用uploadify类型组件,该组件可以在提交表单之前上传文件

create a form.. ex 创建一个表格.. ex

<form name="form" method="post" enctype="multipart/form-data" action="test.php">
<input type="file" name="piture"  value="" onChange="window.document.forms['form'].submit();"/> </form> 

test.php test.php

  if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['picture']['name'])) {  
    $rand=rand(000000000,9999999);
    $fl_name = $_FILES['picture']['name'];
    $expld = explode('.', $fl_name);
    $extnnew = $expld[1];
    $filename = $rand.'.'.$extnnew;  
    $source = $_FILES['picture']['tmp_name'];
    $path_to_image_directory = "uploads/"; 
    if(!is_dir($path_to_image_directory)){
mkdir($path_to_image_directory, 0777);
chmod($path_to_image_directory, 0777);} 

    $target = $path_to_image_directory . $filename;  
    move_uploaded_file($source, $target);  
    $sql ="insert into picture values('','".$filename."','Now()')";
    mysql_query($sql) or die(mysql_error());

                        } 
header('Location:page.php');

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

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