當前位置:首頁 » 文件管理 » net上傳圖片

net上傳圖片

發布時間: 2023-02-15 21:51:29

㈠ .net上傳圖片和讀取圖片

using System;
using System.Drawing;
/// <summary>
/// 功能:文件上傳
/// </summary>
namespace czlib
{
public class UpFile
{
#region------上傳屬性-------
private string _path = @"\UpFile\";//上傳類型
private string _fileType = czlib.weblib.adminlib.web_up();//上傳類型
private int _sizes = 2097152;//傳文件的大小KB(2097152=2MB)
private int _W = 120;//寬300
private int _H = 100;//高300
private bool _smaMap = false;//是否生成縮略圖
private int _mode = 0;//生成方式
/// <summary>
/// 上傳保存路徑
/// </summary>
public string Path
{
set
{
_path = @"\" + value + @"\";
}
}
/// <summary>
/// 允許上傳大小
/// </summary>
public int Sizes
{
set
{
_sizes = value * 1024;//轉為位元組byte
}
}
/// <summary>
/// 允許上傳類型
/// </summary>
public string FileType
{
set
{
_fileType = value;
}
}
/// <summary>
/// 縮略圖寬度
/// </summary>
public int W
{
set { _W = value; }
}
/// <summary>
/// 縮略圖高度
/// </summary>
public int H
{
set { _H = value; }
}
public bool smaMap
{
set { _smaMap = value; }
}
/// <summary>
/// 縮略圖生成方式
/// </summary>
public int mode
{
set { _mode = value; }
}
#endregion
#region-------上傳文件方法------------
/// <summary>
/// 上傳文件
/// </summary>
/// <param name="ful">FileUpload控制項名稱</param>
/// <returns>保存路徑</returns>
public string fileSaveAs(System.Web.UI.WebControls.FileUpload ful)
{
try
{
if (ful.PostedFile.ContentLength == 0) { throw new ApplicationException("請選擇上傳文件!"); }
//LD_2008792347263759274.jpg
System.Random random = new Random();
String modifyFileName = "LD_"+DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + random.Next(1000, 9999);
String uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + _path;
String sourcePath = ful.PostedFile.FileName;
String tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
int strLen = ful.PostedFile.ContentLength;
if (strLen > _sizes)
{
throw new ApplicationException("上傳的圖片不能大於" + _sizes/1024 + "KB");
}
if (_fileType.IndexOf(tFileType + "|") == -1)
{
throw new ApplicationException("目前本系統只能上傳格式為:" + _fileType);
}
//目錄是否存在
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
String filePath = uploadFilePath + modifyFileName + "." + tFileType;
ful.SaveAs(filePath);
if (_smaMap)
{
MakeThumbnail(filePath, uploadFilePath + "small_" + modifyFileName + "." + tFileType);
}
filePath = _path + modifyFileName + "." + tFileType;
return filePath.Replace("\\", "/");
}
catch (Exception ex)
{
throw new ApplicationException("上傳時發生錯誤:" + ex.ToString());
}
}
#endregion
#region---------生成縮略圖方法--------------
/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">實際路徑</param>
/// <param name="thumbnailPath">縮略圖存放實際路徑</param>
public void MakeThumbnail(String originalImagePath, String thumbnailPath)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = _W;
int toheight = _H;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (_mode)
{
case 1://指定寬,高按比例
toheight = originalImage.Height * _W / originalImage.Width;
break;
case 2://指定高,寬按比例
towidth = originalImage.Width * _H / originalImage.Height;
break;
case 3://指定高寬裁減(不變形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * _H / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default://指定高寬縮放(可能變形)
break;
}
//新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置並且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception ex)
{
throw new ApplicationException("成生縮略圖錯誤:" + ex.Message);
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion
}
} 這是上傳並生成縮略圖的代碼,至於瀏覽就簡單了。自己寫寫就可以了

㈡ .net 上傳圖片 我本地能但是客戶端無法上傳

應該是沒有文件寫入許可權,建議將存儲上傳圖片的文件夾在IIS裡面設置成允許讀寫
如果不是許可權的問題,調試一下,看看有什麼是客戶端不存在的

㈢ .net如何上傳圖片並且顯示在頁面上

1.網站根目錄下新建一個 文件夾 UploadFiles
//上傳文件按鈕
protected void btnUpload_Click(object sender, EventArgs e)
{
//定義保存路徑
string savePath = @"~/UploadFiles";

fudTest 是控制項id

//上傳文件
if (fudTest.HasFile)
{
try
{
fudTest.SaveAs(Server.MapPath(savePath) + "\\" + fudTest.FileName);
//可以插入數據

Image1.ImageUrl = "~/UploadFiles/" + fudTest.FileName;

}
catch (Exception ex)
{
lblerror.Text = "發生錯誤:" + ex.Message.ToString();
}
}
else
{
lblMessage.Text = "沒有選擇要上傳的文件!";
}
}

㈣ 在.net 下如何實現圖片的批量上傳

如:有幾個FileUpload控制項有一個確定上傳的ButtonButton的Click寫:protected void Button1_Click(object sender, EventArgs e) { foreach (string fu in Request.Files) { FileUpload FU = form1.FindControl("fu"); if (FU.HasFile) { FU.SaveAs("~/download" + FU.FileName); } } }這是ASP.NET寫的,試過可以,希望對你有幫助!
答案補充
FU.SaveAs("~/download" + FU.FileName);中應該寫成FU.SaveAs("~/download/" + FU.FileName);想必你看出來啦,呵呵一時大意!! 答案補充 ACTIVEX控制項

㈤ ASP.net,如何上傳圖片到服務

常用方法:
前端通過

<form model="post" enctype="multipart/form-data">
<input type="file" name="file"/>
</form>
提供上傳文件的控制項。
後端,直接使用 Request.Files 接收並存儲就行,如:
Request.Files[0].SaveAs("d:\\從客戶端上傳的文件\\"+Request.Files[0].FileName);

㈥ vb.net 圖片上傳

是不是這樣的?

㈦ ASP.NET+C# FILEUPLOAD控制項,如何上傳圖片到伺服器並保存圖片路徑到資料庫

我做了個例子給你x0dx0a前台:x0dx0a

x0dx0a x0dx0a x0dx0a x0dx0a

㈧ .net怎麼把html的圖片上傳到伺服器

把圖片存儲在伺服器(的磁碟中),存儲的過程獲取該圖片所在的路徑然後將該路徑存儲在資料庫中,以後調用的時候就可以根據這個路徑來獲取相應的圖片資源了

㈨ .net Web網頁中怎樣上傳圖片

前台代碼:

<div class="mflLeft">
<div class="mflLeft_FileUpload">
<asp:FileUpload ID="mflFileUpload"
runat="server" BorderColor="#6666FF" Width="150px" />
<asp:Button ID="btnUpload" runat="server" Text="點擊上傳" OnClick="btnUpload_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Image ID="Image1" runat="server" />
</div>
</div>

後台代碼:

protected void btnUpload_Click(object sender, EventArgs e)
{
if (mflFileUpload.HasFile)
{
int a = Convert.ToInt32(ConfigurationManager.AppSettings["maxRequestLength"]);
if (mflFileUpload.PostedFile.ContentLength <= Convert.ToInt32(ConfigurationManager.AppSettings["maxRequestLength"]))
{
string extensionName = Path.GetExtension(mflFileUpload.FileName);
if (CheckFileType(extensionName))
{
Random r = new Random();
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + r.Next(1, 9999) + extensionName;
string filePath = ConfigurationManager.AppSettings["fileUpPath"] + fileName;//圖片上傳路徑
mflFileUpload.SaveAs(Server.MapPath(filePath));//保存上傳的圖片
Image1.ImageUrl = ConfigurationManager.AppSettings["fileUpPath"] + fileName;//顯示上傳的圖片
this.Label1.Text = "文件上傳成功!";
}
else {
Response.Write("<script type='text/javascript'>alert('文件格式不正確!')</script>");
return;
}
}
else {
Response.Write("<script type='text/javascript'>alert('文件大小不能超過4M!')</script>");
return;
}
}
}
protected bool CheckFileType(string fileName)
{
switch (fileName)
{
case ".gif":
return true;
case ".png":
return true;
case ".jpg":
return true;
case ".jpeg":
return true;
default:
return false;
}
}

㈩ ASP.NET上傳控制項上傳圖片

要嗎你就是隨機生成字元串,要嗎就是以時間為名字,精確到毫秒就應該差不多了啊

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:712
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:974
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:685
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:837
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:743
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1085
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:314
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:194
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:882
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:840