Dzwebs.Net

撰写电脑技术杂文十余年

asp.net c#版上传各种文件源代码示例上篇

Admin | 2008-5-20 18:21:45 | 被阅次数 | 7974

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ClassData;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AlaOne.Common;
using System.IO;

namespace AlaOne.Common
{
 /// <summary>
 /// UpFileOnline 的摘要说明。
 /// </summary>
 public class UpFileOnline
 {
  DataConn dc=new DataConn();//数据操作接口
  string strconn=ConfigurationSettings.AppSettings["AlaOne"];//数据库连接接口
  string UploadNewsPath=ConfigurationSettings.AppSettings["UploadNewsPath"];//上传新闻图片保存路径
  string UploadMagPath=ConfigurationSettings.AppSettings["UploadMagPath"];//上传杂志保存路径
  string UploadSwfPath=ConfigurationSettings.AppSettings["UploadSwfPath"];//上传swf文件保存路径
  string UploadVideoPath=ConfigurationSettings.AppSettings["UploadVideoPath"];//上传swf文件保存路径
  string UploadCoverPath=ConfigurationSettings.AppSettings["UploadCoverPath"];//上传杂志封面路径
  string UploadDownPath=ConfigurationSettings.AppSettings["UploadDownPath"];//上传杂志下载路径
  string UploadMusicPath=ConfigurationSettings.AppSettings["UploadMusicPath"];//上传背景音乐路径
  string LeadShowPhoto=ConfigurationSettings.AppSettings["LeadShowPhoto"];//上传LeasShowPhoto
  public UpFileOnline()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  /// <summary>
  /// 判断输入的字符串是否是数字
  /// </summary>
  /// <param name="Value">字符串</param>
  /// <returns>true or false</returns>
  public bool IsNumeric(string Value)
  {
   try
   {
    int i = int.Parse(Value);
    return true;
   }
   catch
   {
    return false;
   }
  }
  /// <summary>
  /// 根据上传文件的名称,生成一个随机图片名称,并上传该图片!
  /// </summary>
  /// <param name="WebFile"></param>
  /// <param name="strFileExt">图片扩展名</param>
  /// <returns>返回该图片名称</returns>
  public string UploadNewsPicsPath(System.Web.UI.HtmlControls.HtmlInputFile WebFile,string strFileExt)
  {
   string strFileName="";
   strFileExt=strFileExt.ToLower();
   if(strFileExt==".jpg")
   {
    RandomNum GetFileCode=new RandomNum();
    strFileName=GetFileCode.PicsID();
    strFileName=strFileName+".jpg";
    UploadPicFile(WebFile,strFileName);
    return strFileName;
   }
   if(strFileExt==".gif")
   {
    RandomNum GetFileCode=new RandomNum();
    strFileName=GetFileCode.PicsID();
    strFileName=strFileName+".gif";
    UploadPicFile(WebFile,strFileName);
    return strFileName;
   }
   if(strFileExt==".bmp")
   {
    RandomNum GetFileCode=new RandomNum();
    strFileName=GetFileCode.PicsID();
    strFileName=strFileName+".bmp";
    UploadPicFile(WebFile,strFileName);
    return strFileName;
   }
   if(strFileExt==".png")
   {
    RandomNum GetFileCode=new RandomNum();
    strFileName=GetFileCode.PicsID();
    strFileName=strFileName+".png";
    UploadPicFile(WebFile,strFileName);
    return strFileName;
   }
   else
   {
    return "";
   }
  }
  public void UploadPicFile(System.Web.UI.HtmlControls.HtmlInputFile WebFile,string strFileName)
  {
   char[] spliter = {'\\'};
   string [] FileName = strFileName.Split(spliter,10);
   string FullPath = UploadNewsPath + @"\" + FileName[FileName.Length-1];  //生成完整文件名
   string fromFullPath = UploadNewsPath + @"\" +"_new"+ FileName[FileName.Length-1];  //生成完整文件名
   WebFile.PostedFile.SaveAs(fromFullPath);  //保存文件

////   string extension=Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
////   string fileName=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
////   string path=Server.MapPath(".")+"/img/"+fileName+extension;
////   UploadFile.PostedFile.SaveAs(path);

   //    //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
   //    System.Drawing.Image image=System.Drawing.Image.FromFile(path);
   //    Graphics g=Graphics.FromImage(image);
   //    g.DrawImage(image,0,0,image.Width,image.Height);
   //    Fontf=newFont("Verdana",32);
   //    Brushb=newSolidBrush(Color.White);
   //    stringaddText=AddText.Value.Trim();
   //    g.DrawString(addText,f,b,10,10);
   //    g.Dispose();


该杂文来自: 网站开发杂文

上一篇:使用正则表达式在NET中动态实现URL超级链接和邮件

下一篇:asp.net c#版上传各种文件源代码示例中篇

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计