本文介绍了上传图像和固定/以&lt Veryfing它的大小;输入类型="文件"&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP .NET 3.5 C#,我喜欢对子级以获取用户试图从

I Use ASP .NET 3.5 C#, and i whould like to get the picture that the user trying to upload from

<输入类型=文件名称=uploadPictureID =uploadPicture> 可我只是用:

Request.Form["uploadPicture"];

和下一步是什么?

决不使用表单文件上传左右出场,
我想保存在我的文件系统文件,并保存在数据库中的路径。

Never played around with uploading files with forms,I wish to save this file on my File System and save the path on the database.

我也需要检查格式,大小和尺寸,如果可能的话甚至可能调整其大小。

I need also to check the format, size and dimensions, and maybe even resize it if possible.

谢谢,

推荐答案

看看下面code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <input type="file" name="uploadPicture" id="uploadPicture">
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default7 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string baseImageLocation = Server.MapPath("Images\\");
        HttpFileCollection uploads = HttpContext.Current.Request.Files;

        HttpPostedFile file = uploads["uploadPicture"];
        string fileExt = Path.GetExtension(file.FileName).ToLower();
        string fileName = Path.GetFileName(file.FileName);
        if (fileName != "")
        {
            if (fileExt == ".jpg" || fileExt == ".gif")
                file.SaveAs(baseImageLocation + fileName);

        }
    }
}

修改

您可以从HttpPostedFile得到的图像尺寸如下

you can get image size from HttpPostedFile as below

  int size = file.ContentLength;

和图像的高度和宽度可以用下面的函数

and for image height and width you can used the following function

 private void GetHeightAndWidht(string image)
    {
        Bitmap bmp = new Bitmap(image);
        int height = bmp.Height;
        int width = bmp.Width;
    }

这篇关于上传图像和固定/以&lt Veryfing它的大小;输入类型=&QUOT;文件&QUOT;&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:39