2013-01-11 36 views
1

我正在使用html 5 <input type="file" accept="image/*;capture=camera"/>文件元素的網絡移動 照片它工作正常,但我怎麼處理file.value發送到Web服務或數據庫? 這裏是移動設備的演示Demo link捕獲的圖像與移動設備的HTML5

這裏是我的代碼,但不工作 我不能得到文件上傳值

<html> 
<script> 
function getPhoto() 
{ 
alert('2'); 
var fu1 = document.getElementById("myfile").value; 
alert("You selected " + fu1); 
} 
</script> 
<body> 
<form> 
<input type="file" name="myfile" accept="image/*;capture=camera"/> 
<input type="submit"/> 
<input type="button" value="get Photo path" onclick="getPhoto()"> 
</form> 
</body> 
</html> 
+0

喜robertc我加入我的代碼 –

+1

應該是'接受=「圖像/ *」捕獲=「相機」'我猜? http://dev.opera.com/articles/view/media-capture-in-mobile-browsers/ –

回答

2

您需要使用File API:http://www.w3.org/TR/FileAPI/

這裏來自html5rocks.com的一個很好的教程:http://www.html5rocks.com/en/tutorials/file/dndfiles/

試着做類似這樣的事

HTML

<input type="file" id="myfile" name="myfile" accept="image/*;capture=camera"/> 

JS

function handleFileSelect(evt) { 
    var files = evt.target.files; // FileList object, files[0] is your file 
} 

document.getElementById('file').addEventListener('change', handleFileSelect, false); 

如果你需要閱讀JS文件,那麼你需要使用的FileReader API。

+1

document.getElementByID('files')或myfile?與他們兩個也給出了鍍鉻「遺漏的類型錯誤:無法調用空的「的addEventListener」錯誤 –

+0

哎呀對不起,這是一個錯字,你應該添加ID =「MYFILE」和的document.getElementById(「MYFILE」) – skyline3000

+1

在哪裏存儲這個文件?我將這與iPhone配合使用,效果非常好。我可以讀取拍攝的照片並將其顯示在DOM中,但我想知道該文件的路徑在哪裏? – climboid

1

你錯過了給你的輸入標籤的ID。 您輸入的標籤會是這樣

<input type="file" name="myfile" id="myfile" accept="image/*;capture=camera"/>