使用JS獲取input file的路徑C:\fakepath\問(wèn)題及解決方法
有時(shí)候,我們給程序添加完一個(gè)功能,在本地測(cè)試是完全可以正常運(yùn)行的,但一發(fā)布到Web上就各種報(bào)錯(cuò),這時(shí)候我們就需要注意啦!
1.問(wèn)題
頁(yè)面有一個(gè)input file服務(wù)器控件,一個(gè)div,div是image標(biāo)簽的容器,當(dāng)點(diǎn)擊input file的值改變,我們往div里追加image標(biāo)簽;
但當(dāng)通過(guò)js的onchange事件動(dòng)態(tài)獲取input file 的路徑的時(shí)候,發(fā)現(xiàn)console.log(path)打印出的路徑是被瀏覽器屏蔽的,例如:C:\fakepath\file.jpg
2.原因
由于瀏覽器的安全機(jī)制,當(dāng)我們獲取input file的路徑時(shí)被fakepath代替,隱藏了真實(shí)物理路徑。
當(dāng)然,調(diào)整瀏覽器的瀏覽器安全設(shè)置可以解決這個(gè)問(wèn)題,但是這種解決辦法顯然不是我們想要的,不可能讓每個(gè)用于都去設(shè)置瀏覽器安全選項(xiàng)。
3.認(rèn)識(shí)window.URL.createObjectURL()
URL.createObjectURL()方法會(huì)根據(jù)傳入的參數(shù)創(chuàng)建一個(gè)指向該參數(shù)對(duì)象的URL,這個(gè)URL的生命僅存在于它被創(chuàng)建的這個(gè)文檔里,新的對(duì)象URL指向執(zhí)行的File對(duì)象或Blob對(duì)象。
語(yǔ)法:objcetURL = window.URL.createObjectURL(file || blob);
參數(shù):File對(duì)象和Blob對(duì)象;File對(duì)象就是一個(gè)文件,比如我用file type="file"標(biāo)簽來(lái)上傳文件,那么里面的每個(gè)文件都是一個(gè)file對(duì)象。Blob對(duì)象就是二進(jìn)制數(shù)據(jù),比如在XMLHttpRequest里,如果指定requestType為blob,那么得到的返回值也是一個(gè)blob對(duì)象。
每次調(diào)用createObjectURL的時(shí)候,一個(gè)新的URL對(duì)象就被創(chuàng)建了。即使你已經(jīng)為同一個(gè)文件創(chuàng)建過(guò)一個(gè)URL.,如果你不再需要這個(gè)對(duì)象,要釋放它,需要使用URL.revokeObjectURL()方法.。當(dāng)頁(yè)面被關(guān)閉,瀏覽器會(huì)自動(dòng)釋放它,但是為了最佳性能和內(nèi)存使用,當(dāng)確保不再用得到它的時(shí)候,就應(yīng)該釋放它。
4.解決辦法
$(document).on('change', '#PictureUrl', function () { //PictureUrl為input file 的id
//console.log(this.files[0]);
function getObjectURL(file) {
var url = null;
if (window.createObjcectURL != undefined) {
url = window.createOjcectURL(file);
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file);
}
return url;
}
var objURL = getObjectURL(this.files[0]);//這里的objURL就是input file的真實(shí)路徑
$('#imgContainer').html("<img src='" + objURL + "' alt='Alternate Text' width='640px' height='350px' id='target' />");
cutImg();//自定義的裁剪圖片函數(shù)
});5.例子
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpFileBox.aspx.cs" Inherits="PublicPage_UpFileBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>文件上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="jquery.Jcrop.min.css" rel="external nofollow" rel="stylesheet" />
<script src="../artDialog/artDialog.source.js?skin=default"></script>
<script src="../artDialog/plugins/iframeTools.js"></script>
<script src="../artDialog/msgBox.js" type="text/javascript"></script>
<script src="jquery.min.js"></script>
<script src="jquery.Jcrop.min.js"></script>
<script src="jquery.color.js"></script>
<style type="text/css">
body {
font: Arial, Helvetica, sans-serif;
color: #000;
line-height: 24px;
font-size: 12px;
}
</style>
<script type="text/javascript">
art.dialog.data('FileName', '<%=FileName %> ');
art.dialog.data('FileSize', '<%=FileSize %> ');
art.dialog.data('FilePath', '<%=FilePath %> ');
</script>
</head>
<body style="background-color: White">
<form id="form1" runat="server">
<table border="0" cellpadding="1" cellspacing="0" style="height: 550px; width: 650px">
<tr height="350">
<td colspan="2">
<div id="imgContainer" style="width: 640px; height: 350px; border: 1px solid #c0c0c0">
<h3>點(diǎn)擊瀏覽按鈕,請(qǐng)選擇要上傳的圖片</h3>
</div>
<input type="hidden" name="x1" id="x1" value="" runat="server" />
<input type="hidden" name="x2" id="x2" value="" runat="server" />
<input type="hidden" name="y1" id="y1" value="" runat="server" />
<input type="hidden" name="y2" id="y2" value="" runat="server" />
<input type="hidden" name="w" id="w" value="" runat="server" />
<input type="hidden" name="h" id="h" value="" runat="server" />
</td>
</tr>
<tr height="30">
<tr height="30">
<td align="left">
<input id="PictureUrl" runat="server" name="File1" type="file" /></td>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<td width="81" align="left">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上傳" Width="60px" CausesValidation="False" /></td>
</tr>
<td class="inputexplain" style="padding-left: 5px" colspan="2">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" BackColor="#FFC0C0"
BorderWidth="1px" ControlToValidate="PictureUrl" Display="Dynamic" ErrorMessage="請(qǐng)選擇您要上傳的文件"
SetFocusOnError="True" Width="138px"></asp:RequiredFieldValidator>
<asp:Label ID="LB_PicError" runat="server" BackColor="#FFC0C0" BorderWidth="1px"
ForeColor="Red" Text="文件上傳失??!" Visible="False" Width="343px"></asp:Label>
<asp:Label
ID="LB_Success" runat="server" BackColor="#C0FFFF" BorderWidth="1px" ForeColor="Teal"
Text="文件上傳成功!" Visible="False" Width="122px"></asp:Label><asp:Label ID="LB_Fail"
runat="server" BackColor="#FFC0C0" BorderWidth="1px" ForeColor="Red" Text="文件上傳失?。?
Visible="False" Width="126px"></asp:Label><br>
<%=hint %></td>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
});
$(document).on('change', '#PictureUrl', function () {
console.log(this.files[0]);
function getObjectURL(file) {
var url = null;
if (window.createObjcectURL != undefined) {
url = window.createOjcectURL(file);
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file);
}
return url;
}
var objURL = getObjectURL(this.files[0]);
$('#imgContainer').html("<img src='" + objURL + "' alt='Alternate Text' width='640px' height='350px' id='target' />");
cutImg();
});
function cutImg() {
var jcrop_api;
$('#target').Jcrop({
bgFade: true,
bgOpacity: .2,
setSelect: [45, 55, 607, 320],
onChange: showCoords,
}, function () {
jcrop_api = this;
});
}
function showCoords(c) {
$('#x1').val(c.x);//通過(guò)<input type='hidden' runat='server'>給后臺(tái)提供選框的寬高
$('#y1').val(c.y);
$('#x2').val(c.x2);
$('#y2').val(c.y2);
$('#w').val(c.w);
$('#h').val(c.h);
};
</script>到此這篇關(guān)于使用JS獲取input file的路徑C:\fakepath\問(wèn)題的文章就介紹到這了,更多相關(guān)js獲取路徑C:\fakepath\內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
bootstrap基本配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了bootstrap基本配置,詳細(xì)講解如何下載并安裝 Bootstrap,討論 Bootstrap 文件結(jié)構(gòu),并通過(guò)一個(gè)實(shí)例演示它的用法。2017-07-07
原生js實(shí)現(xiàn)淘寶購(gòu)物車(chē)功能
這篇文章主要向大家推薦一個(gè)原生js實(shí)現(xiàn)淘寶購(gòu)物車(chē)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
js window.print實(shí)現(xiàn)打印特定控件或內(nèi)容
window.print可以打印網(wǎng)頁(yè),但有時(shí)候我們只希望打印特定控件或內(nèi)容,怎么辦呢?可以把要打印的內(nèi)容放在div中,然后用下面的代碼進(jìn)行打印,希望對(duì)大家有所幫助2013-09-09
JavaScript實(shí)現(xiàn)的購(gòu)物車(chē)效果可以運(yùn)用在好多地方
JavaScript實(shí)現(xiàn)的購(gòu)物車(chē)效果,當(dāng)然這個(gè)效果可以運(yùn)用在好多地方,比如好友的選擇,人力資源模塊等等,需要的朋友可以參考下2014-05-05
javascript中動(dòng)態(tài)加載js文件多種解決辦法總結(jié)
這篇文章主要介紹了javascript中動(dòng)態(tài)加載js文件多種解決辦法,有需要的朋友可以參考一下2013-11-11
詳解JavaScript創(chuàng)建數(shù)組的三種方式
這篇文章主要介紹了JavaScript創(chuàng)建數(shù)組的三種方式:直接聲明,?以對(duì)象方式創(chuàng)建數(shù)組和使用?Array.from()?方法創(chuàng)建,并通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-06-06
使用apply方法實(shí)現(xiàn)javascript中的對(duì)象繼承
javascript中的對(duì)象繼承的方法有很多,在接下來(lái)的文章中為大家介紹下使用apply方法是如何實(shí)現(xiàn)的2013-12-12

