C# ASP.NET 使用 FileUpload如何判断要上传文件大小
ASP.NET(C#)中使用FileUpload上传文件,我想在按“确定上传”之前先判断一下该文件名称是否存在服务器文件夹了,就是说如果存在就提示说该文件已经存在,还有,...
ASP.NET(C#)中使用FileUpload上传文件,我想在按“确定上传”之前先判断一下该文件名称是否存在服务器文件夹了,就是说如果存在就提示说该文件已经存在,还有,我想在按确定按钮之前判断要上传的文件的大小(使用时间当然越短越好),请指教
展开
3个回答
2013-07-10
展开全部
protected void btnup_Click(object sender, EventArgs e)
{
string fileName, fileExtension, filefold,size;
fileName = FileUpload1.FileName.ToString();
fileExtension = System.IO.Path.GetExtension(fileName).ToLower().Replace(".","");
if (FileUpload1.FileContent.Length != 0)
{
if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "gif" || fileExtension == "rar" || fileExtension == "bmp")
{
size = Convert.ToDecimal(FileUpload1.FileContent.Length / 1000).ToString("#0.0");
filefold = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
filefold = filefold + "/";
string path = Server.MapPath("upfile/") + filefold;
DirectoryInfo directory = new DirectoryInfo(path);
if (!directory.Exists) //目录不存在创建
{directory.Create();}
//文件新名称
string name = DateTime.Now.ToString("yyyyMMdd HHmmss");
name = name.Replace("-", "");
name = name.Replace(":", "");
name = name.Replace(" ", "");
name = name.Trim() +"."+ fileExtension;
FileUpload1.SaveAs(path + "/" + name.Trim());
//保存数据
//model_upfile.titleid = Convert.ToInt64(title_id);
model_upfile.useid = STR.NameToId(Session["UserName"].ToString());
model_upfile.oldname = fileName;
model_upfile.newname = name;
model_upfile.Extension = fileExtension;
model_upfile.uptime = DateTime.Now.ToString();
model_upfile.type = type;
model_upfile.Filefold = filefold;
model_upfile.describer = txtms.Text.Trim();
model_upfile.down = 0;
model_upfile.Sizes = size.ToString();
model_upfile.timestring = ViewState["timestring"].ToString();
bll_upfile.Add(model_upfile);
Response.Write("<script>alert('上传文件成功!')</script>");
bind(); csg.BLL.T_User bll_user = new csg.BLL.T_User();
csg.Model.T_User model_user = new csg.Model.T_User();
//给楼主积分
model_user = bll_user.GetModel(STR.NameToId(Session["UserName"].ToString()));
model_user.scores += 1;
bll_user.Update(model_user);
}
else
{
Response.Write("<script>alert('上传失败!\\n只支持jpg|jpeg|gif|bmp|rar\\n如需上传其他文件请压缩后上传!')</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传文件!')</script>");
}
}
{
string fileName, fileExtension, filefold,size;
fileName = FileUpload1.FileName.ToString();
fileExtension = System.IO.Path.GetExtension(fileName).ToLower().Replace(".","");
if (FileUpload1.FileContent.Length != 0)
{
if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "gif" || fileExtension == "rar" || fileExtension == "bmp")
{
size = Convert.ToDecimal(FileUpload1.FileContent.Length / 1000).ToString("#0.0");
filefold = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
filefold = filefold + "/";
string path = Server.MapPath("upfile/") + filefold;
DirectoryInfo directory = new DirectoryInfo(path);
if (!directory.Exists) //目录不存在创建
{directory.Create();}
//文件新名称
string name = DateTime.Now.ToString("yyyyMMdd HHmmss");
name = name.Replace("-", "");
name = name.Replace(":", "");
name = name.Replace(" ", "");
name = name.Trim() +"."+ fileExtension;
FileUpload1.SaveAs(path + "/" + name.Trim());
//保存数据
//model_upfile.titleid = Convert.ToInt64(title_id);
model_upfile.useid = STR.NameToId(Session["UserName"].ToString());
model_upfile.oldname = fileName;
model_upfile.newname = name;
model_upfile.Extension = fileExtension;
model_upfile.uptime = DateTime.Now.ToString();
model_upfile.type = type;
model_upfile.Filefold = filefold;
model_upfile.describer = txtms.Text.Trim();
model_upfile.down = 0;
model_upfile.Sizes = size.ToString();
model_upfile.timestring = ViewState["timestring"].ToString();
bll_upfile.Add(model_upfile);
Response.Write("<script>alert('上传文件成功!')</script>");
bind(); csg.BLL.T_User bll_user = new csg.BLL.T_User();
csg.Model.T_User model_user = new csg.Model.T_User();
//给楼主积分
model_user = bll_user.GetModel(STR.NameToId(Session["UserName"].ToString()));
model_user.scores += 1;
bll_user.Update(model_user);
}
else
{
Response.Write("<script>alert('上传失败!\\n只支持jpg|jpeg|gif|bmp|rar\\n如需上传其他文件请压缩后上传!')</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传文件!')</script>");
}
}
2013-07-10
展开全部
在服务端是没有办法计算的,你可以用js 脚本计算(当然如果浏览器禁用了FSO的组件就用不成了)。 看看下面的例子:<html> <input type="file" name="file1" onchange="ShowSize(this.value)"> </SPAN></li><script language="JavaScript"> </SPAN></li><!-- function ShowSize(files) { var fso,f; fso=new ActiveXObject("Scripting.FileSystemObject"); </SPAN></li> f=fso.GetFile(files); </SPAN></li> var mySize = f.size/1024; </SPAN></li> alert(mySize+" K "); } //--> </SPAN></li></script> </SPAN></li></html> </SPAN></li>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-10
展开全部
try
public static long GetRemoteHTTPFileSize(string sURL)
{
long size = 0L;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
request.Method = "HEAD";
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
size = response.ContentLength;
//size = System.Convert.ToInt64(response.Headers["Content-Length"]);
response.Close();
}
catch
{
size = 0L;
}
return size;
}
public static long GetRemoteHTTPFileSize(string sURL)
{
long size = 0L;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
request.Method = "HEAD";
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
size = response.ContentLength;
//size = System.Convert.ToInt64(response.Headers["Content-Length"]);
response.Close();
}
catch
{
size = 0L;
}
return size;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询