我现有项目的上传文件都保存在程序根目录下的upfile文件夹里了,这样做有很多问题,比如安全,比如影响速度,当然,最直接了当的影响是每次备份的时候都要备份量很大的upfile文件夹而我现在想做将upfile文件夹设在服务器的独立的一个磁盘里:比如,程序在D盘,而上传的数据都在E盘的upfile文件夹下,这要怎么实现?怎么设置这里的路径?

解决方案 »

  1.   

    要获取服务器下的程序根目录比较简单,用HttpContext.Current.Server.MapPath("~")就可以了
    或者用http://webservernameorip(服务器名或IP)/upfile也可以
    但问题是如果设置的路径不在程序根目录下呢?
      

  2.   

    upfile里 指定个绝对路径不就好了吗?
      

  3.   

    下面的代码试试。
    来自http://bingning.net/VB/SOURCE/internet/ftpwebclient.html [C#] // WebClient对象作成
     System.Net.WebClient wc = new System.Net.WebClient();
     //指定用户名和密码
     wc.Credentials = new System.Net.NetworkCredential("username", "password");
     //向FTP服务器上传文件
     wc.UploadFile("ftp://localhost/test.txt", @"C:\test.txt");
     //向FTP服务器上传文件
     wc.Dispose();
      

  4.   

    Use appSetting in app.config save the setting value
      

  5.   

    在app.config中配置,怎么配置?总不能设成E:\upfile\……或者是http://webservername/……
      

  6.   

    //每一个用户指定一个专用目录
    private FileAttr SaveFile(HttpPostedFile postFile,string strUserID)
        {
            FileAttr fileAttr = new FileAttr();
            //RootDir
            if (!System.IO.Directory.Exists(this.SaveFileRoot))
                System.IO.Directory.CreateDirectory(this.SaveFileRoot);        #region Info        string strFileName = postFile.FileName;
            //要存储的文件全名称 路径+文件名   ==== SaveFileRoot + strUserID + FileName
            string strSaveFileFullPath = this.SaveFileRoot + strUserID + "\\";
            if (!System.IO.Directory.Exists(strSaveFileFullPath))
                System.IO.Directory.CreateDirectory(strSaveFileFullPath);        //
            string strFileSortFileName = postFile.FileName;
            strFileSortFileName = strFileSortFileName.Substring(strFileSortFileName.LastIndexOf('\\')+1);
            strSaveFileFullPath += strFileSortFileName;        postFile.SaveAs(strSaveFileFullPath);
            int dFileSize = postFile.ContentLength;
     
            fileAttr.FullName = strSaveFileFullPath;
            fileAttr.Name = strFileSortFileName;
            fileAttr.FileSize = dFileSize;
            #endregion        return fileAttr;
        }
      

  7.   


    为什么不能是E:\upfile\?就是在这里写绝对路径的。让客户写。他机器什么配置就写什么。你一开始给个缺省值就可以了
      

  8.   

    我要的就是这个SaveFileRoot该怎么设置,我的程序在D盘,但我想将文件上传至E盘,该如何获取或者设置这里的路径呢?我有一个想法,如果在E盘建一个upfile文件夹,然后在iis设置虚拟目录,通过http://webservername/upfile这样可以访问吗?
    有人试过吗?
      

  9.   

    http://topic.csdn.net/u/20090330/11/658de515-135e-4b24-b554-8fcd08dd7404.html
      

  10.   


    if (!System.IO.Directory.Exists("E\:upfile")) 
                System.IO.Directory.CreateDirectory(E\:upfile); 
      

  11.   

    回wk122348545:指定虚拟目录不可行
    回llsen:路径应该为虚拟路径
      

  12.   

    我用类似这样的代码测试
    if (!System.IO.Directory.Exists("http://127.0.0.1/upfile")) 提示“不支持URI格式”,该怎么改?
      

  13.   

    上来说一声:
    用@"\\127.0.0.1\upfile\test"这样的路径可以访问,给aspnet或者network service用户足够的权限也可以进行读写操作,其中,将upfile文件夹设置成虚拟目录在本地上是可行的,至于在上传至服务器上是否可行有待于测试
      

  14.   

    先设置路径
    然后用SaveAs()保存
      

  15.   

    Server.MapPath("upfile/");
    这个可以!