上传图像并使用<输入类型=“文件”>修复/检查其大小?

发布于 2024-11-04 09:05:52 字数 312 浏览 0 评论 0 原文

上传的图片

我使用 ASP .NET 3.5 C#,我希望获取用户尝试从 我可以只使用:

Request.Form["uploadPicture"];

接下来怎么办?

从来没有玩过用表单上传文件, 我希望将此文件保存在我的文件系统上并将路径保存在数据库上。

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

谢谢, 担

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

<input type="file" name="uploadPicture" id="uploadPicture"> Can i just use:

Request.Form["uploadPicture"];

And what next?

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.

Thanks,
Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-11-11 09:05:52

像这样使用

HttpFileCollection files = Request.Files;

Use like this

HttpFileCollection files = Request.Files;
绅刃 2024-11-11 09:05:52

查看以下代码

<%@ 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 获取图像大小,如下所示

  int size = file.ContentLength;

,对于图像高度和宽度,您可以使用以下函数

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

Check out the following 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);

        }
    }
}

EDIT

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文