asp.2.0关于FileUpload控件
在asp.2.0中,FileUpload控件选择上传路径时,关于(stringipath=Server.MapPath("image")+"\\"+name;)中,在上传...
在asp.2.0中,FileUpload控件选择上传路径时,关于(string ipath = Server.MapPath("image") + "\\" + name;)中,在上传图片时,参数“image”必须和要上传的图片所在的文件夹名称相同,否则就不行!
请问:我们在做网页时,要对方随便在哪里都可以上传,参数应该怎样设置???或者是用其他的方法??
string name = fupFhoto.FileName;
string size = fupFhoto.PostedFile.ContentLength.ToString();
string type = fupFhoto.PostedFile.ContentType;
string type2=name.Substring(name.LastIndexOf(".")+1);
string ipath = Server.MapPath("image") + "\\" + name;
string fpath = Server.MapPath() + "\\" + name;
string wpath = "image\\" + name;
if (type2 == "jpg" || type2 == "gif")
{
fupFhoto.SaveAs(ipath);
Image1.ImageUrl = wpath;
Label1.Text = "你传的文件是:" + name + "<br>大小是" + size + "<br>类型是" + type;
}
else
{
Image1.Visible = false;
fupFhoto.SaveAs(fpath);
Label1.Text = "你传的是什么啊?";
}
}
}
-------------------------
能不能给段代码,举个例子!!
再三感谢!!! 展开
请问:我们在做网页时,要对方随便在哪里都可以上传,参数应该怎样设置???或者是用其他的方法??
string name = fupFhoto.FileName;
string size = fupFhoto.PostedFile.ContentLength.ToString();
string type = fupFhoto.PostedFile.ContentType;
string type2=name.Substring(name.LastIndexOf(".")+1);
string ipath = Server.MapPath("image") + "\\" + name;
string fpath = Server.MapPath() + "\\" + name;
string wpath = "image\\" + name;
if (type2 == "jpg" || type2 == "gif")
{
fupFhoto.SaveAs(ipath);
Image1.ImageUrl = wpath;
Label1.Text = "你传的文件是:" + name + "<br>大小是" + size + "<br>类型是" + type;
}
else
{
Image1.Visible = false;
fupFhoto.SaveAs(fpath);
Label1.Text = "你传的是什么啊?";
}
}
}
-------------------------
能不能给段代码,举个例子!!
再三感谢!!! 展开
2个回答
展开全部
public static string FileUpAndReturnGetNewFileNameRAR(Page p, FileUpload fileUpload, string ver)
{//根据上传页面,上传控件,语言,返回上传后新文件的名称
string newfilename = "";
if (fileUpload.HasFile)
{
string fileExt =
System.IO.Path.GetExtension(fileUpload.FileName);
try
{
newfilename = ver + "_" + GetNewFileName(fileUpload.FileName.ToString()).ToLower();
if (File.Exists(p.Server.MapPath("~/Uploads/" + newfilename)) == false)
{
fileUpload.SaveAs(p.Server.MapPath("~/Uploads/" + newfilename));
}
else
{
//newfilename = ver + "_" + GetNewFileName(fileUpload.FileName.ToString()).ToLower();
//fileUpload.SaveAs(p.Server.MapPath("~/Uploads/" + newfilename));
FunctionClass.ErrorMsg(p, "该文件已经存在!");
}
}
catch (Exception ex)
{
FunctionClass.ErrorMsg(p.Page, "错误:: " + ex.Message.ToString());
}
}
return newfilename;
}
public static string GetNewFileName(string FileName)
{//跟据文伯名产生一个由时间+随机数组成的一个新的文件名
string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString()
//+ rand.Next(1000).ToString()
+ FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
return newfilename;
}
{//根据上传页面,上传控件,语言,返回上传后新文件的名称
string newfilename = "";
if (fileUpload.HasFile)
{
string fileExt =
System.IO.Path.GetExtension(fileUpload.FileName);
try
{
newfilename = ver + "_" + GetNewFileName(fileUpload.FileName.ToString()).ToLower();
if (File.Exists(p.Server.MapPath("~/Uploads/" + newfilename)) == false)
{
fileUpload.SaveAs(p.Server.MapPath("~/Uploads/" + newfilename));
}
else
{
//newfilename = ver + "_" + GetNewFileName(fileUpload.FileName.ToString()).ToLower();
//fileUpload.SaveAs(p.Server.MapPath("~/Uploads/" + newfilename));
FunctionClass.ErrorMsg(p, "该文件已经存在!");
}
}
catch (Exception ex)
{
FunctionClass.ErrorMsg(p.Page, "错误:: " + ex.Message.ToString());
}
}
return newfilename;
}
public static string GetNewFileName(string FileName)
{//跟据文伯名产生一个由时间+随机数组成的一个新的文件名
string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString()
//+ rand.Next(1000).ToString()
+ FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
return newfilename;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询