0

上传之前,如何使用 jQuery验证宽度和高度

<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>
4

4 回答 4

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");
        }
    }
}
于 2012-04-10T05:58:14.460 回答
0

在客户端无法验证要上传的图像的宽度和高度。

但是上传后,您可以在此获取图像的宽度和高度:

var height = $("#Image").height()
var width = $("#Image").width()
于 2012-04-10T05:58:32.197 回答
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!");
    }

}

(或者)您可以尝试使用此上传器,您可以在用户上传之前确定常见的宽度和高度,这适用于 Chrome、Firefox 和 IE

于 2012-04-10T08:33:36.813 回答
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;
    }
}
于 2012-04-11T04:33:42.027 回答