Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何上传html格式的图像_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何上传html格式的图像

Javascript 如何上传html格式的图像,javascript,jquery,html,Javascript,Jquery,Html,我是设计新手,我想上传html页面中的图像。我正在使用这个代码 <form name="myWebForm" action="mailto:youremail@email.com" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="500" /> <input type="file" name="uploadField" /> </form> 我的问题是:我们如何上

我是设计新手,我想上传html页面中的图像。我正在使用这个代码

<form name="myWebForm" action="mailto:youremail@email.com" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="500" />
<input type="file" name="uploadField" />
</form>


我的问题是:我们如何上传html格式的图像或浏览图像?

在您的示例中,您将发布到mailto链接。Formdata需要发布到处理您的请求的服务器。这可以是任何类型的编程语言,例如php、python、perl、.net等

当服务器页面收到post请求时,它可以处理“post数据”。在这里,您可以编程服务器应该如何处理您发送的数据。例如,可以存储在数据库中、存储在硬盘上或发送带有图像的电子邮件等

下面是一个简单的例子:

HTML,例如index.HTML

action=“action.php”
指服务器上处理您的帖子的文件

<form action="action.php" method="post" enctype="multipart/form-data">
 <input name="uploadedfile" type="file" />
 <input type="submit" />
</form>
这会将发布的文件移动到服务器harddsik上的/upload_files/myimage.jpg


请参见

这是如何在Javascript中实现的:

   var filesList = document.getElementById("uploadField");
    fileList.onchange = function(e){
        var file = e.target.files; // The file object may contain multiple files
        data = new FormData();
        data.append("nameOfFile",file[0]); // Here i send the 1st file
        xhr = new XMLHttpRequest();
        xhr.open("post", "UploadFile.php", true);
        xhr.send(data);
    }
下面是一个链接,其中有一个关于如何使用HTML5文件API的好例子: 我得到了我的Ans:

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">

var thumbWidth = 400, thumbHeight =455 ;

    function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)
                        .width(thumbWidth)
                        .height(thumbHeight);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
</script>

<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <input type='file' onchange="readURL(this);" />
    <img id="blah" src="#" alt="your image" />
</body>
</html>

变量thumbWidth=400,thumbHeight=455;
函数readURL(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$(#废话)
.attr('src',e.target.result)
.宽度(指宽)
.高度(指高);
};
reader.readAsDataURL(input.files[0]);
}
}
JS-Bin
文章、旁白、数字、页脚、页眉、H组、,
菜单,导航,部分{显示:块;}

仅使用html无法上载。您还需要在服务器上处理上传的文件。“action”用于指导您要发送要处理的数据的页面。@C.S.做一个更好的老师:“将
action
属性添加到
表单
元素,如
action=“process.php“
以所需的.php文件为目标,在该文件中,您拥有处理从POST方法接收的数据的代码。;)举例说明如何处理上传图像(即与问题相关的内容)可能更有帮助。对不起,我的问题与文件示例不同。请参考:
<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">

var thumbWidth = 400, thumbHeight =455 ;

    function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)
                        .width(thumbWidth)
                        .height(thumbHeight);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
</script>

<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <input type='file' onchange="readURL(this);" />
    <img id="blah" src="#" alt="your image" />
</body>
</html>