2012-04-10 114 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; 
    } 
}