<input id="File_Audio" type="file" /> 一步到位上传图片、音频等文件
首先我们需要建一个上传文件的类UploadFile.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace SMS.Web
{
/******************************************************************
** 文件名:UploadFile.cs
** 创建人:sundy zhangzhao
** 日 期: 2008-4-25
** 修改人:
** 日 期:
** 描 述: 上传文件公共类
** 版 本: 1.0
**----------------------------------------------------------------
******************************************************************/
/// <summary>
/// 用于上传文件
/// </summary>
public class UploadFile
{
#region 初始化
protected HtmlInputFile upFile;
protected string upNewFileName = "";
public string upNewFilePath = "";
#endregion
#region 方法
/// <summary>
/// 初始化文件
/// </summary>
public UploadFile(HtmlInputFile agFile)
{
upFile = agFile;
}
/// <summary>
/// 保存文件
/// </summary>
public void saveFile(string filepath)
{
try
{
upNewFileName = GetTimestr() + GetExpName(".");
upNewFilePath = filepath + "\\" + upNewFileName.ToLower();
upFile.PostedFile.SaveAs(upNewFilePath);
}
catch (System.Exception Ex)
{
throw Ex;
}
}
/// <summary>
/// 返回当前时间的字符串
/// </summary>
public string GetTimestr()
{
string strTime = DateTime.Now.ToString("yyyyMMddHHmmss");
strTime = strTime + Get_Str_Num(3);
return strTime;
}
/// <summary>
/// 判断是否具有提交的文件
/// </summary>
public bool CheckFileContent()
{
if (upFile.PostedFile.ContentLength == 0)
return false;
else
return true;
}
/// <summary>
/// 校验上传文件的文件名
/// </summary>
/// <param name="checkstr">输入字符分割的特征值</param>
public string GetExpName(string checkstr)
{
string upFileName = upFile.PostedFile.FileName.ToLower();
int pos = upFileName.LastIndexOf(checkstr);
string s = upFileName.Substring(pos);
if (s.ToLower() == ".jpg")//jpg 改成jpeg 后缀名
{
s = ".jpeg";
}
return s;
}
/// <summary>
/// 获得上传文件的大小
/// </summary>
public int getUpFileSize()
{
return upFile.PostedFile.ContentLength;
}
/// <summary>
/// 获得上传文件的文件名
/// </summary>
public string getNewFileName()
{
return upNewFileName.ToLower();
}
/// <summary>
/// 删除该文件
/// </summary>
public void delFile(string FileName)
{
if (File.Exists(FileName))
{
File.Delete(FileName);
}
}
#endregion
#region 获取随机数的方法
public string Get_Str_Num(int strnum)
{
string BmpStr = "";
string passchars = "0123456789";
int iRandNum;
Random random = new Random(GetRandomSeed());
for (int i = 0; i < strnum; i++)
{
iRandNum = random.Next(passchars.Length);
BmpStr += passchars[iRandNum];
}
return BmpStr;
}
public int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
#endregion
}
}
前台HTML 代码
<input id="File_Audio" type="file" class="input_zpsck" name="File_Audio" runat="server" onmousemove="this.style.cursor='hand'" onchange="File_View_Audio(this);" style="width: 200px" />
<input type="hidden" id="dotype_commend" name="dotype_commend" />
前台JS 代码
function File_View_Audio(obj) {
var fName = obj.value;
var idx = fName.lastIndexOf(".");
var expName = fName.substring(idx + 1);
expName = expName.toLowerCase();
if (expName == "mp3" || expName == "wav" || expName == "mid") {
document.getElementById('dotype_commend').value = "uploadAudio";
document.getElementById('form1').submit();
}
else {
alert("文件格式不正确!请选择正确的音频格式!");
}
}
后台C#代码:
定义全局变量
UploadFile upFile;
public string dotype_commend = "";
在 Page_Load 方法中调用
protected void Page_Load(object sender, EventArgs e)
{
if (Request["dotype_commend"] != null)
{
dotype_commend = Request["dotype_commend"].ToString();
}
if (dotype_commend.Equals("uploadAudio"))//上传音乐
{
string audio_url = "";
if (File_Audio != null)
{
audio_url = File_Audio.Value.Trim();
}
HtmlInputFile FileName = new HtmlInputFile();
FileName = File_Audio;
upFile = new UploadFile(FileName);
if (audio_url.Length > 0)
{
if (upFile.getUpFileSize() / 1024 < 50)
{
string strFilePath = upFile.getNewFileName().ToString();
string strExtension = upFile.GetExpName(".").ToString().ToLower();
//string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;//文件名重命名
string strPath = "../portraits/audio/";//图片路径
string strFileSaveAsPath = Server.MapPath(strPath);
upFile.saveFile(strFileSaveAsPath);
#endregion
}
else
{
//showmsg += "alert('音频文件不能大于50K!');";
MessageBox.Show(this.Page, "音频文件不能大于50K!");
}
}
}
}
浙公网安备 33010602011771号