如何在HTML中引用上传的img src
How to reference to an uploaded img src in HTML
我正在创建一个响应式移动网站,我希望能够从设备上拍摄并上传照片。我已经使用处理了这个部分
<form method="post" action="takephoto.php" enctype="multipart/form-data">
<input type="file" id="myfile" name="myfile" accept="image/*;capture=camera"/>
</form>
现在,我遇到的问题是,在我选择了要使用的图片后,我不知道以后如何在代码中引用它。我需要这样做的原因是我使用的是.js库,它允许我上传ISBN条形码的图片,.js文件会读取这个文件,并将ISBN作为文本吐出,我稍后会做更多的处理。(可能将此链接到Google Books API)
这就是我获取条形码扫描仪.js的地方:http://badassjs.com/post/654334959/barcode-scanning-in-javascript
我对这一切还比较陌生,谢谢你的帮助。
您将不得不使用ajax。此链接包含一些有用的信息。
使用ajax上传图片,并在响应中返回url
或"失败"。然后你可以在你的js代码中使用
if(response=="failed"){
//Handle upload failed
}
else {
//Handle url. Here response is your url actually.
// So to append the image to a given id, you would use,
$("#append_to_id").append("<img src='"+response+"'>");
}
由于您使用的JavaScript库正在解析页面上的图像,你必须在上传后在页面上显示图像。
所以基本上你需要4个步骤来实现这一点:
- 展示上传表格(这是你在问题中所做的)
- 获取步骤1中上传的图像并将其保存到可访问的位置
- 在屏幕上显示图像
- 调用get_barcode_from_image.js进行解析
这里有一段代码供参考。它包含以上所有4个步骤。
<?php
// step 2: save the image
if ($_FILES['myfile']['error'] == 0) {
move_uploaded_file($_FILES['myfile']['tmp_name'], 'u/barcode.jpg');
?>
<!-- step 3: show the image -->
<img src="u/barcode.jpg" id="barcode">
<script src="get_barcode_from_image.js"></script>
<div id="result"></div>
<!-- step 4: parse the barcode -->
<button onclick='document.getElementById("result").innerHTML = getBarcodeFromImage("barcode")'>scan</button>
<?php
}
?>
<!-- step 1: show the upload form -->
<form method="post" action="barcode.php" enctype="multipart/form-data">
<input type="hidden" name='a' value='b'/>
<input type="file" id="myfile" name="myfile" accept="image/*;capture=camera"/>
<input type="submit" value="submit" />
</form>
当然,您可以使用ajax来获得更好的用户体验,但过程不会改变。
相关文章:
- Javascript:使用绝对路径设置img src
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 插入“;img src"在javascript中
- 从二维数组中获取img src和img维度,并在body中显示
- 在不发出请求的情况下设置img src
- Jquery/Javascript使用IF/ELSE语句更改img SRC
- 使用JQuery从包含A href值中设置*all*IMG-src值
- Django,HTML,JS:<img src=“#">可以在HTML中工作,但不能在JS文件中工作
- Img-src只通过javascript更改一次
- 正则表达式使链接可点击(仅在“href”而不是“img src”中)
- 交换 img src 或显示/隐藏多个图像是否更快
- 如果有办法在 N 秒内加载未触发,则有没有办法设置 img src
- img src 在 Firefox 和 IE 的 jQuery 脚本中没有更改
- 如何从弹簧控制器方法更新html img src
- onload 不会在 Firefox 中使用 createObjectURL img.src 触发
- img src 属性在为空时返回页面的 URL
- EmberJS config prod img src url
- Jquery-如何在悬停时更改img src
- writeln img src将数据提交到外部系统-Firefox和IE挂起
- 使用jQuery禁用img src等于特定值时的href