上传文件代码问题
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UploadFile : System.Web.UI.Page
{
string strUser;
protected void Page_Load(object sender, EventArgs e)
{
strUser = Request.QueryString["username"];
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFlieName;
string strFileType;
Random ran = new Random();
//filename由时间加三位随机数组成
string strFileISN = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(0, 999);
string strSq1;
int iFileSize;
if (FileUpload1.PostedFile.FileName != null)
{
//存在上传文件
strFlieName = FileUpload1.PostedFile.FileName;
strFileType = strFlieName.Substring(strFlieName.LastIndexOf(".") + 1);
strFlieName = strFlieName.Substring(strFlieName.LastIndexOf("\\") + 1);
iFileSize = FileUpload1.PostedFile.ContentLength;
try
{
#region 判断保存的文件是否存在
string strUpPath = @"uploadfiles\";
//文件夹不存在的时候,创建文件夹
if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
}
string strUrl = Server.MapPath(strUpPath + @"\" + this.FileUpload1.FileName);
FileUpload1.SaveAs(strUrl);
lblResult.Text = "文件上传成功";
#endregion
}
catch
{
lblResult.Text = "文件上传失败";
} 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UploadFile : System.Web.UI.Page
{
string strUser;
protected void Page_Load(object sender, EventArgs e)
{
strUser = Request.QueryString["username"];
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFlieName;
string strFileType;
Random ran = new Random();
//filename由时间加三位随机数组成
string strFileISN = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(0, 999);
string strSq1;
int iFileSize;
if (FileUpload1.PostedFile.FileName != null)
{
//存在上传文件
strFlieName = FileUpload1.PostedFile.FileName;
strFileType = strFlieName.Substring(strFlieName.LastIndexOf(".") + 1);
strFlieName = strFlieName.Substring(strFlieName.LastIndexOf("\\") + 1);
iFileSize = FileUpload1.PostedFile.ContentLength;
try
{
#region 判断保存的文件是否存在
string strUpPath = @"uploadfiles\";
//文件夹不存在的时候,创建文件夹
if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
}
string strUrl = Server.MapPath(strUpPath + @"\" + this.FileUpload1.FileName);
FileUpload1.SaveAs(strUrl);
lblResult.Text = "文件上传成功";
#endregion
}
catch
{
lblResult.Text = "文件上传失败";
} 展开
1个回答
展开全部
/*前台*/
string imageurl = (new Dispose_Image()).upLoadImage(file_uploadimage, "/UploadImage/Banner"); //file_uploadimage为上传控件ID,第二个参数为需要传到的目录
(new Dispose_Image()).upLoadThumbnail(Server.MapPath(imageurl), "/UploadImage/HotPiont/Big", 308, 76);//送你个压缩
/// <summary>
/// 错误描述
/// </summary>
public string error = "";
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filepath">原文件路径</param>
/// <param name="directory ">文件保存目录</param>
/// <returns>保存文件全路径(上传失败:返回空)</returns>
public string upLoadImage(FileUpload fileupload, string directory)
{
if (fileupload == null || fileupload.PostedFile.FileName.Trim() == "")
{
error = "原路径不存在";
return "";
}
string filepath = fileupload.PostedFile.FileName.Trim();
if (!check_ExtendName(filepath))
{
error = "该文件不是图片格式的文件";
return "";
}
if (!Directory.Exists(HttpContext.Current.Server.MapPath(directory))) Directory.CreateDirectory(HttpContext.Current.Server.MapPath(directory));
directory += "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000) + Path.GetExtension(filepath);
System.Drawing.Image img = null;
try
{
img = System.Drawing.Image.FromStream(fileupload.PostedFile.InputStream);
img.Save(HttpContext.Current.Server.MapPath(directory));
img.Dispose();
return directory;
}
catch
{
error = "该文件损坏或丢失";
return "";
}
}
/// <summary>
/// 判断所选的文件是不是允许上传的文件
/// </summary>
/// <param name="extendName"></param>
/// <returns></returns>
private bool check_ExtendName(string filepath)
{
string extName = System.IO.Path.GetExtension(filepath);
string[] extendNames = { ".jpg", ".jpge", ".gif", ".bmp", ".png" };
foreach (string tempextName in extendNames)
{
if (tempextName.Trim().ToLower() == extName.Trim().ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="SavePath">原图的完整路径</param>
/// <param name="sThumb">缩略图名的文件夹名</param>
/// <param name="sSavePath">生成缩略图的路径</param>
/// <param name="intThumbWidth">缩略图的宽度</param>
/// <param name="intThumbHeight">缩略图的高度</param>
/// <param name="sextendName">缩略图的后缀名</param>
/// <returns></returns>
public string upLoadThumbnail(string SavePath, string sSavePath, int intThumbWidth, int intThumbHeight)
{
string name = System.IO.Path.GetFileName(SavePath);
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(string.Format("{0}/{1}", sSavePath, name));
MagickNet.Image img = new MagickNet.Image(SavePath);
System.Drawing.Image image = System.Drawing.Image.FromFile(SavePath);
image.Dispose();
img.Resize(new System.Drawing.Size(intThumbWidth, intThumbHeight));
img.Write(smallImagePath);
MagickNet.Magick.Term();
return "";
}
追问
你好 我私信了你一下,能帮我看看是哪里出问题了吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询