将输入保存到Parse(JavaScript)中 [英] Save input into Parse (Javascript)

查看:56
本文介绍了将输入保存到Parse(JavaScript)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当用户选择文件时,该文件将直接上传到Parse中.我现在添加了一对输入文本字段,例如个人名称,仅当用户单击选择按钮时才希望同时记录到解析"中的地址,从现在开始,一旦用户选择了文件,它将自动提交

Currently when the user select a file, it gets directly uploaded into Parse. I have added now couple input text field such as name of individual, address that I would want to be recorded into Parse at the same time only when the user has click the select button as of now its automatically submitted once the user has selected the file.

$(document).ready(function() {
  // ***************************************************
  // NOTE: Replace the following your own keys
  // ***************************************************
  Parse.initialize("id", "id");

  function saveDocumentUpload(objParseFile) {
    var documentUpload = new Parse.Object("Scan");
    documentUpload.set("Name", "");

    documentUpload.set("DocumentName", objParseFile);
    documentUpload.save(null, {
      success: function(uploadResult) {
        // Execute any logic that should take place after the object is saved.
        var photo = uploadResult.get("profileImg");
        $("#profileImg")[0].src = photo.url();
      },
      error: function(uploadResult, error) {
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and description.
        alert('Failed to create new object, with error code: ' + error.description);
      }
    });

   
  }

  $('#documentFileUpload').bind("change", function(e) {
    var fileUploadControl = $("#documentFileUpload")[0];
    var file = fileUploadControl.files[0];
    var name = file.name; //This does *NOT* need to be a unique name
    var parseFile = new Parse.File(name, file);

    parseFile.save().then(
      function() {
        saveDocumentUpload(parseFile);
      },
      function(error) {
        alert("error");
      }
    );
  });
});

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<form>
  <input type="file" id="documentFileUpload">
  <br />
  <input type="text" value="UserID">
  <br />
  <input type="text" value="Address">
  <br />
  <input type="submit" id="documentFileUpload" value="submit">
</form>

更新2:

    <HTML>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {

      // ***************************************************
      // NOTE: Replace the following your own keys
      // ***************************************************

        Parse.initialize("id", "id");

      function saveDocumentUpload(objParseFile)
      {
         var documentUpload = new Parse.Object("Scan");
         documentUpload.set("Name", "");

         documentUpload.set("DocumentName", objParseFile);
         documentUpload.save(null, 
         {
            success: function(uploadResult) {
              // Execute any logic that should take place after the object is saved.

            },
            error: function(uploadResult, error) {
              // Execute any logic that should take place if the save fails.
              // error is a Parse.Error with an error code and description.
              alert('Failed to create new object, with error code: ' + error.description);
            }
         });
      }

      $('#documentFileUploadButton').bind("click", function (e) {
            var fileUploadControl = $("#documentFileUpload")[0];
            var file = fileUploadControl.files[0];
            var name = file.name; //This does *NOT* need to be a unique name
            var parseFile = new Parse.File(name, file);

            var user_id = $('#user_id').val();

            var address = $('#address').val();


            parseFile.set('UserId', user_id);
            parseFile.set('Address', address);


            parseFile.save().then(
                    function () {
                        saveDocumentUpload(parseFile);
                    },
                    function (error) {
                        alert("error");
                    }
            );
        });
    });
    </script>

    <body><form>
        <input type="file" id="documentFileUpload">
        <br/>
        <input type="text" placeholder="UserID" id="user_id">
        <br/>
        <input type="text" placeholder="Address" id="address">
        <br/>
        <input type="submit" id="documentFileUploadButton" value="submit">
    </form>
    </body>
    </HTML>

**Updated 2:**
<HTML>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

  // ***************************************************
  // NOTE: Replace the following your own keys
  // ***************************************************

    Parse.initialize("pWG7YizRnwxRjplGT9RSLoHtFItDtvmc2EK0YJAe", "C2qlan3y2PXi6nwVbACGT6fY3CTus8oVEvNo889u");

   function saveDocumentUpload(objParseFile)
  {
     var documentUpload = new Parse.Object("Scan");
     documentUpload.set("Name", "");

     documentUpload.set("DocumentName", objParseFile);

     var user_id = $('#user_id').val();

     var address = $('#address').val();

     // create a pointer by assigning just an ID
     var user = new Parse.User();
     user.id = user_id;

     documentUpload.set('User', user);
     documentUpload.set('Address', address);

     documentUpload.save(null, 
     {
        success: function(uploadResult) {
          // Execute any logic that should take place after the object is saved.

        },
        error: function(uploadResult, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and description.
          alert('Failed to create new object, with error code: ' + error.description);
        }
     });
  }

  $('#documentFileUploadButton').bind("click", function (e) {
        var fileUploadControl = $("#documentFileUpload")[0];
        var file = fileUploadControl.files[0];
        var name = file.name; //This does *NOT* need to be a unique name
        var parseFile = new Parse.File(name, file);

        var user_id = $('#user_id').val();

        var address = $('#address').val();


        parseFile.set('UserId', user_id);
        parseFile.set('Address', address);


        parseFile.save().then(
                function () {
                    saveDocumentUpload(parseFile);
                },
                function (error) {
                    alert("error");
                }
        );
    });
});
</script>

<body><form>
    <input type="file" id="documentFileUpload">
    <br/>
    <input type="text" placeholder="UserID" id="user_id">
    <br/>
    <input type="text" placeholder="Address" id="address">
    <br/>
    <input type="submit" id="documentFileUploadButton" value="submit">
</form>
</body>
</HTML>

推荐答案

您两次拥有"documentFileUpload":

更改:

<form>
    <input type="file" id="documentFileUpload">
    <br />
    <input type="text" value ="UserID"><br />
    <input type="text" value ="Address"> <br />
    <input type="submit"  id="documentFileUpload" value="submit">

</form>

收件人:

<form>
    <input type="file" id="documentFileUpload">
    <br />
    <input type="text" value ="UserID"><br />
    <input type="text" value ="Address"> <br />
    <input type="submit"  id="documentFileUploadButton" value="submit">

</form>

修改

要回答有关记录UserId和Address字段的评论,请参见以下代码.我将文件上传绑定更改为按钮,并绑定了click事件.这样可以解决您选择上传文件的问题.

To answer your comment about the recording of the UserId and Address fields, please see the following code. I changed the file upload binding to the button and bound the click event. This will fix your file uploading on selection.

此外,还添加了ID的user_id和地址,以允许jQuery从这些字段中获取值:

Also, added the id's user_id, and address to allow jQuery to get the values from those fields:

$('#documentFileUploadButton').bind("click", function (e) {
        var fileUploadControl = $("#documentFileUpload")[0];
        var file = fileUploadControl.files[0];
        var name = file.name; //This does *NOT* need to be a unique name
        var parseFile = new Parse.File(name, file);

        var user_id = $('#user_id').val();

        var address = $('#address').val();

        parseFile.set('UserId', user_id);
        parseFile.set('Address', address);

        parseFile.save().then(
                function () {
                    saveDocumentUpload(parseFile);
                },
                function (error) {
                    alert("error");
                }
        );
    });

然后:

<form>
    <input type="file" id="documentFileUpload">
    <br/>
    <input type="text" value="UserID" id="user_id">
    <br/>
    <input type="text" value="Address" id="address">
    <br/>
    <input type="submit" id="documentFileUploadButton" value="submit">
</form>

这篇关于将输入保存到Parse(JavaScript)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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