不使用参数检索上载文件的值

时间:2015-12-28 06:57:21

标签: javascript jquery html asp.net-mvc-4

目标:
我想要第二个输入名称" file2"使用最新版本的FF,CHrome和IE,支持jquery代码自动上传到actionresult。

问题:
它不起作用,因为HttpPostedFileBase file2为null。它应该包含基于上传文件的任何值,并支持jquery。

的信息:
*它适用于IE,Chrome和FF会很棒 *有一个目的,我要问这个不寻常的方法 * jquery和html的源代码仅适用于FF。

谢谢!



public void SetViewModelValues(CreatePostViewModel viewModel)
{
    Title = viewModel.Title;
    Thumbnail = viewModel.ThumbnailId != null ? db.Images.Find(viewModel.ThumbnailId) : null;
}

$("#file1id").change(function () {
  if($('#file2id').length){
  	 $('#file2id').remove();
  }
  
  
  $(this).clone().attr('id', 'file2id').insertAfter($(this));
});




<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input id="file1id" name="file" type="file" accept=".xml, .txt" />

<input id="file2id" name="file2" type="file" accept=".xml, .txt" />
public ActionResult UploadShipments(HttpPostedFileBase file, HttpPostedFileBase file2)
{


---
}
<input id="file1id" name="file" type="file" accept=".xml, .txt" />

<input id="file2id" name="file2" type="file" accept=".xml, .txt" />
$("#file1id").change(function () {
  if($('#file2id').length){
     $('#file2id').remove();
  }


  $(this).clone().attr('id', 'file2id').insertAfter($(this));
});

2 个答案:

答案 0 :(得分:0)

如果您的javascript代码有效,请尝试使用Request.Files对象。它包含从客户端

收到的服务器的所有文件

答案 1 :(得分:0)

 var fileInput = document.getElementById('id-input-file-2');

            var ext = fileInput.files[0].name.substring(fileInput.files[0].name.lastIndexOf('.'));
            if (ext.toLowerCase() == ".docx") {
                var filename = fileInput.files[0].name;
                formdata.append(fileInput.files[0].name, fileInput.files[0]);
                var xhr = new XMLHttpRequest();
                xhr.open('POST', '@Url.Action("uploadDocument", "Transcription")' + "?jobid=" + job_id + "&transid=" + trans_id);
                xhr.send(formdata);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {...
控制器中的

 public ActionResult uploadDocument(string jobid, string transid)
    {
            int count = Request.Files.Count;
            if (count == 1)
            {
                HttpPostedFileBase file = Request.Files[0];
                var name = Path.GetFileName(file.FileName);
            }

您将获得有关控制器上传文件的所有信息