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

この方法を試すことができます:

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

アップロードする画像の幅と高さをクライアント側で検証することはできません。

ただし、アップロード後、これで画像の幅と高さを取得できます。

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

サーバー側 (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 に答える