多个文件上传到不同的目标文件夹 [英] multiple file uploading for different destination folder
本文介绍了多个文件上传到不同的目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面有2个..文件上传控件和一个按钮,我希望它们的目标文件夹必须不同.我该怎么做...代码如下:-
I have page of having 2 .. file upload controls and a button i want that their destination folder must be different.How can i do this... the code is as follows:-
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string fn1 = Guid.NewGuid() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
string sp = Server.MapPath("pics/houses/Housemodel");
sp += "/" + fn1;
FileUpload1.PostedFile.SaveAs(sp);
obj.housemodel = fn1;
}
if (FileUpload2.HasFile)
{
string fn2 = Guid.NewGuid() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
string sp = Server.MapPath("pics/houses/Architecture");
sp += "/" + fn1;
FileUpload1.PostedFile.SaveAs(sp);
obj.architecture = fn2;
}
}
有人帮帮我..它给我错误..字符文字太多字符
Somebody Help me out.. it is giving me error.. too many character for character literal
推荐答案
就您的第一个要求而言,您几乎已经拥有它,您只需要检查您的组件/变量名称.
As far as your first requirement you almost had it you just need to check your component/variable names.
if (FileUpload2.HasFile)
{
string fn2 = Guid.NewGuid() + System.IO.Path.GetExtension(FileUpload2.FileName);
string sp = Server.MapPath("pics/houses/Architecture");
sp += "/" + fn2;
FileUpload2.PostedFile.SaveAs(sp);
obj.architecture = fn2;
}
还要注意使用System.IO.Path.GetExtension
来获取文件的扩展名,它比substring/lastindex方法要干净一些.
对于您的错误,您需要指定错误发生的位置.
Also notice the use of System.IO.Path.GetExtension
to get the extension of the file, it''s a bit cleaner than the substring/lastindex method.
As for your error you need to specify where it occurs.
这篇关于多个文件上传到不同的目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文