0

我想在 javascript 中捕获上传文件属性数组。这可以在javascript中完成吗?

详细要求:我有 html 表单,由 javascript 函数提交,现在我想在该表单中再添加一个字段上传文件,但是如何通过javascript 函数将上传文件数组发送到我的php 脚本

$('#save').click(function(){        
    var filearray = new Array();
    var file = $("#file")[0].files[0];
    filearray["filename"] = file.name;
    filearray["filesize"] = file.size;
    filearray["fileType"] = file.type;
    filearray["fileTemp"] = file.tmp_name;
    filearray["fileError"] = file.error;
});

我没有在 javascript 中获取属性tmp_name错误,因为它在文件位于服务器上时生成,但我在客户端执行此操作。

4

4 回答 4

3

文件属性tmp_name和文件在服务器上时生成的错误

于 2012-12-29T10:12:58.653 回答
0

尝试这样的事情

var extension = new Array(".jpg",".jpeg",".JPEG",".JPG",".PNG",".png",".ico",".ICO",".GIF",".gif");

function CheckExtension(fieldvalue)
{
        var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
    for(var i = 0; i < extension.length; i++) {
        if(thisext == extension[i]) { return true; }
        }

    return false;

}

将字段值作为输入文件值传递。

希望这可以帮助你..

于 2012-12-28T06:31:18.117 回答
0

获取文件名和扩展名:

var extension = fileName.split('.').pop();
var name = fileName.split('.')[0];

获取文件大小:

$('#youfiletextbox').bind('change', function() {
  var fileSize = this.files[0].size);
});
于 2012-12-28T06:35:46.063 回答
0
  <input type="file" class='img-icon' id="qImage" name="qImage1" accept="image/*" onchange="GetFileInfo('qImage')"/>  
function GetFileInfo(imageID) {
        var fileInput = document.getElementById(imageID);
        var fName;
        var fSize;
        var fType;

        var message = "";
        if ('files' in fileInput) {
            if (fileInput.files.length == 0) {
                alert("Please browse for one or more files.");
            } else {
                for (var i = 0; i < fileInput.files.length; i++) {
                    var file = fileInput.files[i];
                    if ('name' in file) {
                        fName = file.name;
                    }
                    else {
                        fName = file.fileName;
                    }
                    if ('size' in file) {
                        fSize = file.size;
                    }
                    else {
                        fSize = file.fileSize;
                    }
                    if ('type' in file) {
                        fType = file.type;
                    }
                    else {
                        fType = file.mediaType;
                    }
                }

                var extension = fName.split('.').pop();
                var size = fSize / 1024;

                if(extension == "png" || extension == "jpg" || extension == "svg"){
                    if(size > 5){
                        alert("Image size must be less than 5kb.");
                        document.getElementById(imageID).value = null;
                    }
                } else {
                    alert("Image type must png, jpg or svg and size must be less than 5kb.");
                    document.getElementById(imageID).value = null;
                } 


            }
        } 
        else {
            if (fileInput.value == "") {
                message += "Please browse for one or more files.";
                message += "<br />Use the Control or Shift key for multiple selection.";
            }
            else {
                message += "Your browser doesn't support the files property!";
                message += "<br />The path of the selected file: " + fileInput.value;
            }
            document.getElementById ("info").innerHTML = message;
        }
    }
<div id="info" style="margin-top:20px"></div>
于 2019-07-25T15:05:14.017 回答