本文介绍了上载图片并使用“输入类型="“文件""来修复/调整尺寸吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用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
<输入类型=文件" name ="uploadPicture" id ="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.
谢谢,丹
推荐答案
查看以下代码
<%@ 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;
}
这篇关于上载图片并使用“输入类型="“文件""来修复/调整尺寸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!