2012-04-10 176 views
0

,然後再上傳之前驗證圖像尺寸,我如何驗證寬度和高度與jQueryjQuery的圖片上傳,如何上傳

<form id="form1" runat="server"> 
    <asp:FileUpload id="FileUploadControl" runat="server" /> 
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" /> 
    <br /><br /> 
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " /> 
</form> 
+0

什麼問題,或者說它在哪裏? – V4Vendetta 2012-04-10 05:31:52

+0

檢查客戶端上傳的任何圖像的高度和寬度是不可能的。可能會有一些jQuery插件,試着用google搜索,但這個插件的體積很大。爲了避免頁面大小問題,您必須驗證服務器端的圖像高度和寬度。如果你建議你會得到代碼。 – Murtaza 2012-04-10 05:53:53

+0

你可以用簡單的英語解釋你需要什麼嗎? – Murtaza 2012-04-10 05:58:16

回答

0

您可以使用jQuery的uploadify插件。

下面是示例代碼我有asp.net

<script type="text/javascript"> 
    // <![CDATA[ 
    var id = "55"; 
    var theString = "asdf"; 
    $(document).ready(function() { 
    $('#fileInput').uploadify({ 
    'uploader': 'uploadify/uploadify.swf', 
    'script': 'Upload.ashx', 
    'scriptData': { 'id': id, 'foo': theString}, 
    'cancelImg': 'uploadify/cancel.png', 
    'auto': true, 
    'multi': true, 
    'fileDesc': 'Image Files', 
    'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg', 
    'queueSizeLimit': 90, 
    'sizeLimit': 4000000, 
    'buttonText': 'Choose Images', 
    'folder': '/uploads', 
    'onAllComplete': function(event, queueID, fileObj, response, data) { 

    } 
}); 

});

然後你想一個處理程序(ashx的):

public class Upload : IHttpHandler, IRequiresSessionState 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     try 
     { 
      HttpPostedFile file= context.Request.Files["Filedata"]; 

      int id = (Int32.Parse(context.Request["id"])); 
      string foo = context.Request["foo"]; 
      file.SaveAs("C:\\" + id.ToString() + foo + file.FileName); 

      context.Response.Write("1"); 
     } 
     catch(Exception ex) 
     { 
      context.Response.Write("0"); 
     } 
    } 
} 
+0

uploadify不工作chrome firefox IE.http://www.uploadify.com/demos/ – PMD 2012-04-10 08:04:58

0

驗證到要上傳圖像的寬度和高度是無法實現的客戶端。

但上傳後,你可以在這個寬度和圖像的高度:

var height = $("#Image").height() 
var width = $("#Image").width() 
+0

FileUpload控件是用asp.net而不是jquery處理的。 – undefined 2012-04-10 06:08:51

+0

此外,您可以使用javascript Image()對象 - http://www.proglogic.com/learn/javascript/lesson10.php – rickyduck 2012-04-10 08:31:08

0

你可以試試這個方法:

private void ButtonUpload_Click(object sender, System.EventArgs e) { 

    //Determine type and filename of uploaded image 
    string UploadedImageType = UploadedPicture.PostedFile.ContentType.ToString().ToLower(); 
    string UploadedImageFileName = UploadedPicture.PostedFile.FileName; 

    //Create an image object from the uploaded file 
    System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(UploadedPicture.PostedFile.InputStream); 

    //Determine width and height of uploaded image 
    float UploadedImageWidth = UploadedImage.PhysicalDimension.Width; 
    float UploadedImageHeight = UploadedImage.PhysicalDimension.Height; 

    //Check that image does not exceed maximum dimension settings 
    if (UploadedImageWidth > 600 || UploadedImageHeight > 400) { 
      Response.Write("This image is too big - please resize it!"); 
    } 

}

(或者)你可以請嘗試使用此uploader,您可以在用戶上傳之前確定常見的寬度和高度,這可以在Chrome,Firefox和IE中使用

0

服務器端(C#);

using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUploadJcrop.PostedFile.InputStream)) 
{ 
    if (myImage.Width > 500) 
    { 
     Double d = (myImage.Height * 500)/myImage.Width; 
     Int32 myH = (Int32)Math.Round(d, 0); 
     Bitmap src = Bitmap.FromStream(FileUploadJcrop.PostedFile.InputStream) as Bitmap; 
     Bitmap result = new Bitmap(500, myH); 
     using (Graphics myg = Graphics.FromImage((System.Drawing.Image)result)) 
     { 
      myg.DrawImage(src, 0, 0, 500, myH); 
     } 
     result.Save(savePath); 
     FileSaved = true; 
    } 
}