主頁(yè) > 知識(shí)庫(kù) > ASP.NET圖片上傳實(shí)例(附源碼)

ASP.NET圖片上傳實(shí)例(附源碼)

熱門標(biāo)簽:西寧電銷外呼系統(tǒng)公司 海東防封電銷卡 云南外呼系統(tǒng)代理 辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作 安陸市地圖標(biāo)注app 聊城智能電銷機(jī)器人電話 南昌自動(dòng)外呼系統(tǒng)線路 寧德防封版電銷卡 上海市三維地圖標(biāo)注

由于需要圖片上傳的功能,所以花了一些時(shí)間網(wǎng)上找相關(guān)資料終于搞定,效果圖如下:

下面的是解決方案截圖和上傳的圖片截圖:

下面是代碼:
1.界面代碼

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %>

!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>
 style type="text/css">
 .pic_text{ color:Red;}
 .pic_label { color:Gray; margin-top:5px; margin-bottom:5px;}
 .pic_image { margin:5px;}
 /style>
/head>
body>
 form id="form1" runat="server">
 div class="pic_image">asp:Image ID="pic" runat="server" />/div>
 div>asp:FileUpload ID="pic_upload" runat="server" />asp:Label ID="lbl_pic" runat="server" class="pic_text">/asp:Label>/div>
 div class="pic_label">上傳圖片格式為.jpg, .gif, .bmp,.png,圖片大小不得超過(guò)8M/div>
 div>asp:Button ID="btn_upload" runat="server" Text="上傳" onclick="btn_upload_Click"/>/div>
 /form>
 
/body>
/html>


2.后臺(tái)代碼

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

namespace Pic_Try
{
 public partial class UploadPic : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

 }

 protected void btn_upload_Click(object sender, EventArgs e)
 {
  Boolean fileOk = false;
  if (pic_upload.HasFile)//驗(yàn)證是否包含文件
  {
  //取得文件的擴(kuò)展名,并轉(zhuǎn)換成小寫
  string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
  //驗(yàn)證上傳文件是否圖片格式
  fileOk = IsImage(fileExtension);

  if (fileOk)
  {
   //對(duì)上傳文件的大小進(jìn)行檢測(cè),限定文件最大不超過(guò)8M
   if (pic_upload.PostedFile.ContentLength  8192000)
   {
   string filepath = "/images/";
   if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就創(chuàng)建file文件夾
   {
    Directory.CreateDirectory(Server.MapPath(filepath));
   }
   string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//這是存到服務(wù)器上的虛擬路徑
   string mappath = Server.MapPath(virpath);//轉(zhuǎn)換成服務(wù)器上的物理路徑
   pic_upload.PostedFile.SaveAs(mappath);//保存圖片
   //顯示圖片
   pic.ImageUrl = virpath;
   //清空提示
   lbl_pic.Text = "";
   }
   else {
   pic.ImageUrl = "";
   lbl_pic.Text = "文件大小超出8M!請(qǐng)重新選擇!";
   }
  }
  else {
   pic.ImageUrl = "";
   lbl_pic.Text = "要上傳的文件類型不對(duì)!請(qǐng)重新選擇!";
  }
  }
  else
  {
  pic.ImageUrl = "";
  lbl_pic.Text = "請(qǐng)選擇要上傳的圖片!";
  }
 }

 /// summary>
 /// 驗(yàn)證是否指定的圖片格式
 /// /summary>
 /// param name="str">/param>
 /// returns>/returns>
 public bool IsImage(string str) {
  bool isimage = false;
  string thestr = str.ToLower();
  //限定只能上傳jpg和gif圖片
  string[] allowExtension = { ".jpg", ".gif", ".bmp",".png" };
  //對(duì)上傳的文件的類型進(jìn)行一個(gè)個(gè)匹對(duì)
  for (int i = 0; i  allowExtension.Length; i++)
  {
  if (thestr == allowExtension[i])
  {
   isimage = true;
   break;
  }
  }
  return isimage;
 }

 /// summary>
 /// 創(chuàng)建一個(gè)指定長(zhǎng)度的隨機(jī)salt值
 /// /summary>
 public string CreateSalt(int saltLenght)
 {
  //生成一個(gè)加密的隨機(jī)數(shù)
  RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  byte[] buff = new byte[saltLenght];
  rng.GetBytes(buff);
  //返回一個(gè)Base64隨機(jī)數(shù)的字符串
  return Convert.ToBase64String(buff);
 }

 
 /// summary>
 /// 返回加密后的字符串
 /// /summary>
 public string CreatePasswordHash(string pwd, int saltLenght)
 {
  string strSalt = CreateSalt(saltLenght);
  //把密碼和Salt連起來(lái)
  string saltAndPwd = String.Concat(pwd, strSalt);
  //對(duì)密碼進(jìn)行哈希
  string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
  //轉(zhuǎn)為小寫字符并截取前16個(gè)字符串
  hashenPwd = hashenPwd.ToLower().Substring(0, 16);
  //返回哈希后的值
  return hashenPwd;
 }
 }
}

3.最后防止上傳大文件圖片時(shí)報(bào)錯(cuò),配置文件添加配置

?xml version="1.0" encoding="utf-8"?>

!--
 如何配置 ASP.NET 應(yīng)用程序的詳細(xì)消息
 -->

configuration>
 system.web>
 compilation debug="true" targetFramework="4.0" />
 httpRuntime executionTimeout="240" maxRequestLength="8192000"/>
 /system.web>

/configuration>

ASP.NET圖片自動(dòng)上傳和局部刷新顯示的源碼下載。

希望大家喜歡這篇文章。

您可能感興趣的文章:
  • ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
  • ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法
  • asp.net fileupload控件上傳圖片并預(yù)覽圖片
  • asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
  • Asp.Net上傳圖片同時(shí)生成高清晰縮略圖
  • asp.net圖片上傳實(shí)例
  • ASP.net WebAPI 上傳圖片實(shí)例
  • Asp.Net平臺(tái)下的圖片在線裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
  • 基于asp.net實(shí)現(xiàn)圖片在線上傳并在線裁剪功能

標(biāo)簽:青海 崇左 洛陽(yáng) 南寧 汕尾 衢州 贛州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET圖片上傳實(shí)例(附源碼)》,本文關(guān)鍵詞  ASP.NET,圖片,上傳,實(shí)例,附,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET圖片上傳實(shí)例(附源碼)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET圖片上傳實(shí)例(附源碼)的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章