私は ASP.NET C# 4.0 を使用しています。私の Web フォームは、画像をアップロードするための入力タイプ ファイルで構成されています。
イメージを SQL データベースに保存する前に、クライアント側でイメージを検証する必要があります
input type= file を使用して画像をアップロードしているため、画像のサイズ、寸法を検証するためにページをポストバックしたくありません。
あなたの助けが必要です ありがとう
私は ASP.NET C# 4.0 を使用しています。私の Web フォームは、画像をアップロードするための入力タイプ ファイルで構成されています。
イメージを SQL データベースに保存する前に、クライアント側でイメージを検証する必要があります
input type= file を使用して画像をアップロードしているため、画像のサイズ、寸法を検証するためにページをポストバックしたくありません。
あなたの助けが必要です ありがとう
あなたはこのようなことをすることができます...
これは、W3Cの新しいファイル APIをサポートするブラウザーで実行できます。インターフェイスのreadAsDataURL
関数を使用しFileReader
、データ URL を に割り当てますsrc
(その後、画像のとをimg
読み取ることができます)。現在、Firefox 3.6 は File API をサポートしており、Chrome と Safari は既にサポートしている、またはサポートしようとしていると思います。height
width
したがって、移行フェーズ中のロジックは次のようになります。
ブラウザーがファイル API をサポートしているかどうかを検出します (これは簡単です: if (typeof window.FileReader === 'function')
)。
その場合は、データをローカルで読み取り、画像に挿入して寸法を見つけます。
そうでない場合は、ファイルをサーバーにアップロードし (おそらく、ページを離れるのを避けるために iframe からフォームを送信します)、サーバーをポーリングして画像の大きさを尋ねます (または、必要に応じて、アップロードされた画像を尋ねるだけです)。
編集私はしばらくの間、File API の例を作成するつもりでした。ここに1つあります:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Show Image Dimensions Locally</title>
<style type='text/css'>
body {
font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function loadImage() {
var input, file, fr, img;
if (typeof window.FileReader !== 'function') {
write("The file API isn't supported on this browser yet.");
return;
}
input = document.getElementById('imgfile');
if (!input) {
write("Um, couldn't find the imgfile element.");
}
else if (!input.files) {
write("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
write("Please select a file before clicking 'Load'");
}
else {
file = input.files[0];
fr = new FileReader();
fr.onload = createImage;
fr.readAsDataURL(file);
}
function createImage() {
img = document.createElement('img');
img.onload = imageLoaded;
img.style.display = 'none'; // If you don't want it showing
img.src = fr.result;
document.body.appendChild(img);
}
function imageLoaded() {
write(img.width + "x" + img.height);
// This next bit removes the image, which is obviously optional -- perhaps you want
// to do something with it!
img.parentNode.removeChild(img);
img = undefined;
}
function write(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
}
</script>
</head>
<body>
<form action='#' onsubmit="return false;">
<input type='file' id='imgfile'>
<input type='button' id='btnLoad' value='Load' onclick='loadImage();'>
</form>
</body>
</html>
Firefox 3.6 でうまく動作します。私はそこでライブラリを使用することを避けたので、属性 (DOM0) スタイルのイベント ハンドラーなどについてはお詫びします。
function getImgSize(imgSrc) {
var newImg = new Image();
newImg.onload = function() {
var height = newImg.height;
var width = newImg.width;
alert ('The image size is '+width+'*'+height);
}
newImg.src = imgSrc; // this must be done AFTER setting onload
}