doushi3819244 2015-10-23 14:22
浏览 229

Angularjs在表单中提交表单以进行文件上传

I have a page that has a form on it. One of the things I need to include on the page is the ability to upload a file. I am trying to do this by using another form with an in it. When the user selects the file, I want to get the file name and then perform the actual upload. From what I read, I cannot submit a form within a form, because the submit button in the inner form will also submit the outer form. I have tried looking for options and found the following page.

Can I trigger a form submit from a controller?

I thought that if I used this type of directive, then submitted it with the commit, then it should work....my problem is that for some reason when I try to access the form in my controller, it is showing as undefined. I think I need the form so that I can get the file name of the selected file...there may be a different way to achieve this than what I have tried but I haven't found it yet...

The latest controller, directive, and HTML files are below.

Controller:
$scope.uploadIt = function($form) {
    console.log($form);
    if ($form.$valid) {
       $form.commit();
    }
};

HTML:
<form editable-form name="editableForm" onaftersave="update" >
   <tr>
       :  other fields here....
   </tr>
   <tr>
      <td>
        <form  ng-form-commit name='fileForm' action="scripts/upload_file.php" method="post" enctype="multipart/form-data">
            <label for="exampleInputFile">File input</label>
            <input type="file" id="file" name="file">
            <p class="help-block">Upload a file to link it with this project.</p>
            <input type="text" name="prgGuid" id="prgGuid" value="{{programId}}" style="display:none;">
            <input type="button" ng-click="uploadIt(fileForm)" name="submit" value="Upload File" class="btn btn-default">
        </form>
    </td>
  </tr>
</form>

Directive:
app.directive("ngFormCommit", [function(){
  return {
    require:"form",
    link: function($scope, $el, $attr, $form) {
        $form.commit = function() {
            $el[0].submit();
        };
    }
};

}]);

One more this that I will need to do....when the file is actually uploaded, I will actually be changing the file name so it is unique. Once the file is uploaded, I will need to pass back the new file name to my controller so I can store/display it. So I will need the original name the user selected and the new name...my php file will pass it back. I was thinking at first that maybe I could use a $http.post to submit the form and get the information back, but I couldn't get the file name the user selected. I was thinking if I could use the http.post to process the php file, then I wouldn't run into the form inside form, submit issue.

Please can someone tell me the easiest way to accomplish uploading a file with the form inside form constraints I have? I have been trying various things from posts I have seen but on here but I haven't found anything similar....please help. Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据