有什麼方法可以在將圖像大小(高度和寬度)上載到服務器之前進行驗證?ASP.NET:在上傳之前驗證文件大小(寬度和高度)
我認爲是使用Javascript,但我不知道如何。或者也許用一些客戶端的asp.net驗證器。
有什麼建議嗎?
有什麼方法可以在將圖像大小(高度和寬度)上載到服務器之前進行驗證?ASP.NET:在上傳之前驗證文件大小(寬度和高度)
我認爲是使用Javascript,但我不知道如何。或者也許用一些客戶端的asp.net驗證器。
有什麼建議嗎?
這不能在客戶端使用JavaScript來完成。也許你會發現一個用flash,silverlight或類似軟件編寫的文件上傳組件,允許通過類型,大小和尺寸來限制上傳的文件。
你可以使用GD來檢查大小看this project一個ASP.net包裝
在客戶端? – leppie 2009-10-16 10:56:20
沒有服務器端(asp.net) – 2009-10-16 10:58:37
-1 - 他特別說「上傳到服務器之前」 – 2009-10-16 11:03:08
你需要問問自己,你真的認爲這是可能的呢?
Javascript將會怎樣打開一個文件?它會如何讀取它?你有一個圖書館,可以閱讀任何數量的圖像格式?
你根本無法知道客戶端的JS文件大小。
什麼你可能做的是檢查的文件大小,一旦請求到達服務器上,然後取消轉移,如果預期的文件大小超過一定的限度:
在上載模塊的BeginRequest:
HttpWorkerRequest workerRequest = (HttpWorkerRequest)context.GetType().GetProperty("WorkerRequest", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(context, null);
// Indicates if the worker request has a body
if (workerRequest.HasEntityBody())
{
// Get the byte size of the form post.
long contentLength = long.Parse((workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength)));
if (contentLength > MAX_UPLOAD_FILE_SIZE * 1024)
{
workerRequest.CloseConnection();
context.Response.Redirect(SomeErrorPage);
}
}
沒有測試還沒有,但理論上它可能工作
編輯:沒關係,我是個白癡。我以爲他的意思是檢查文件大小,而不是圖像大小
我發現this Javascript snippet並在我的系統上工作(IE 7.0.6001.18000)。你應該知道並檢查可能的跨瀏覽器問題。
var tempImage;
function showDimensions() {
var imageName = document.forms[0].elements['myFile'].value;
if (imageName != '') {
imageName = 'file:///' + escape(imageName.split('\\').join('/'));
imageName = imageName.split('%3A').join(':');
tempImage = new Image();
tempImage.onload = getDimensions;
tempImage.src = imageName + '?randParam=' + new Date().getTime();
// append a timestamp to avoid caching issues - which happen if you
// overwrite any image with one of different dimensions, and try to
// get the dimensions again even with cache settings to max,
// in both ff and ie!
}
}
function getDimensions() {
alert(tempImage.width + ' x ' + tempImage.height);
}
+1 - 或者silverlight當然:) – 2009-10-16 11:02:32