PostedFile.SaveAs(string filename),可以保存为相对路径吗?如果不行由别的方法吗?,50分,在线等

basil 2003-08-23 11:17:34
PostedFile.SaveAs(string filename),可以保存为相对路径吗?如果不行由别的方法吗?,50分,在线等
...全文
159 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2003-08-23
  • 打赏
  • 举报
回复
不可以
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
酋长 2003-08-23
  • 打赏
  • 举报
回复
根据你应用程序的路径,设置相应的路径;"../"表示退一级目录
binapex 2003-08-23
  • 打赏
  • 举报
回复
记忆中好像不行,你可以获取应用程序基础路径啊,然后根据判断来实现与相对路径相似的效果。
控件UpdloadFile文件上传eg: string newfilename = file_uploadid.FileName; string size = file_uploadid.PostedFile.ContentLength.ToString(); string type = file_uploadid.PostedFile.ContentType; string type2 = newfilename.Substring(newfilename.LastIndexOf(".") + 1); string path = ""; try { if (file_uploadid.PostedFile != null && file_uploadid.PostedFile.FileName != "") { string hzm = System.IO.Path.GetExtension(file_uploadid.PostedFile.FileName);//后缀名 如 .doc string[] a = { ".txt", ".jpg", ".jpeg", ".gif", ".png", ".docx", ".doc", ".xlsx", ".xls", ".rar", ".zip", ".pdf" };//设定好了的格式 if (!a.Contains(hzm)) { Response.Write("文件格式不正确"); } else { int defaulsize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["filesize"]);//取得设置的默认文件的大小 int filesize = (file_uploadid.PostedFile.ContentLength) / 1024; //取得上传的文件的大小,单位为bytes if (filesize < defaulsize) { #region 对文件进行操作 newfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + hzm;//文件的新名字 如20120711105734222.doc path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile//");//文件保存的路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } #endregion } else { //超过了文件的大小 Response.Write("上传的文件超过了3000M,请重新选择 "); } } } } catch (Exception) { Response.Write("文件格式不正确"); } #endregion if (newfilename != "") { file_uploadid.SaveAs(path + newfilename); //保存文件 }
using system; using system.web.ui; using system.web.ui.webcontrols; using system.componentmodel; using system.web.ui.htmlcontrols; using system.io; using system.drawing; using system.drawing.design; namespace yingnet.common { /// /// fileupload 的摘要说明。e:\program\common\fileupload.bmp /// [toolboxbitmap(typeof(yingnet.common.fileupload), "fileupload.bmp"), defaultproperty("text"), defaultevent("click"), toolboxdata("<{0}:fileupload runat=server>fileupload>")] public class fileupload : system.web.ui.webcontrols.webcontrol { /// /// 上传按钮 /// private button button=new button(); /// /// 上传文件个数 /// private int filenum=1; /// /// file对象 /// private htmlinputfile[] file; /// /// 保存路径,默认为系统的临时目录 /// private string path=system.io.path.gettemppath(); /// /// 上传的文件名组 /// private string[] filename; /// /// 后缀文件名组 /// private string[] suffix; /// ///过滤器,写法是.txt;.abc /// private string filter=""; /// /// 限制文件上传大小,为0是不限制,单位是字节 /// private int size=0;//system.componentmodel.defaulteventattribute /// /// 上传事件 /// [bindable(true),category("事件"),description("上传后激发的事件") ] public event eventhandler click; /// /// 上传文件数 /// [bindable(true), category("参数"),description("设定上传文件的个数"), defaultvalue("1")] public int filenum{ set{ if(value<1){ value=1; } filenum=value; this.controls.clear(); file=new htmlinputfile[filenum]; filename=new string[filenum]; suffix=new string[filenum]; for(int i=0;i<filenum;i++) { file[i]=new htmlinputfile(); this.controls.add(file[i]); } this.controls.add(button); } get{ return filenum; } } /// /// 上传按钮的文本 /// [bindable(true), category("参数"), description("设定上传文件的路径"), defaultvalue("1")] /// /// 上传路径 /// public string uploadpath { set{ if("".equals(value)||value==null){ value=system.io.path.gettemppath(); } path=value; } get{ return path; } } /// /// 得到文件名 /// public string[] filename{ get{ return filename; } } /// /// 得到后缀 /// public string[] suffix{ get{ return suffix; } } /// /// 过滤器 /// public string filter{ set{ filter=value; } get{ return filter; } } /// /// 限制大小 /// public int filesize{ set{ size=value; } get{ return size; } } /// /// 快捷键 /// public override string accesskey{ get{ return button.accesskey; } set{ button.accesskey=value; } } /// /// 背景 /// public override system.drawing.color backcolor{ get{ return button.backcolor; } set{ button.backcolor=value; } } /// /// 边框颜色 /// public override system.drawing.color bordercolor{ get{ return button.bordercolor; } set{ button.bordercolor=value; } } /// /// 边框风格 /// public override borderstyle borderstyle{ get{ return button.borderstyle; } set{ button.borderstyle=value; } } /// /// 文本 /// [bindable(true), category("appearance"), defaultvalue("")] public string text { get { return button.text; } set { button.text = value; } } public fileupload():base(){ filenum=1; button.click+=new eventhandler(this.button_click); this.controls.add(button); } /// /// 按钮事件 /// /// /// private void button_click(object sender, eventargs e){ upload(); ///添加你的代码 if(click!=null) click(sender,e); ///抛处事件 } /// /// 上传 /// private void upload(){ system.web.httppostedfile postedfile; for(int i=0;i<filenum;i++){ try{ postedfile=file[i].postedfile; if(postedfile!=null) { if(postedfile.contentlength>size && size!=0){ break; } string suf=getsuffix(postedfile.filename); if(filter!=null && filter.indexof(suf)>-1){ break; } filename[i]=datetime.now.ticks.tostring(); suffix[i]=suf; postedfile.saveas(system.io.path.combine(path,filename[i]+suf)); } }finally{ filename[i]=""; } } } /// /// 获取后缀名 /// /// 文件名 /// 返回带.的后缀名 private string getsuffix(string filename){ int index=filename.lastindexof("."); if(index>0){ return filename.substring(index); } return ""; } } }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧