首页 新闻 搜索 专区 学院

急:一个ajax上传的问题

0
悬赏园豆:40 [已解决问题] 解决于 2013-05-07 17:45


public string UpLoadFile(string fileNamePath, string toFilePath)
    {
        try
        {
            //获取要保存的文件信息
            FileInfo file = new FileInfo(fileNamePath);
            //获得文件扩展名
            string fileNameExt = file.Extension;
            if (file.Length > 41943040)
            {
                return "0|errorfile|上传文件不能超过40M";
            }
            //验证合法的文件
            if (CheckFileExt(fileNameExt))
            {
                //生成将要保存的随机文件名
                string fileName = GetFileName() + fileNameExt;
                //检查保存的路径 是否有/结尾
                if (toFilePath.EndsWith("/") == false) toFilePath = toFilePath + "/";

                //按日期归类保存
                string datePath = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd") + "/";
                if (true)
                {
                    toFilePath += datePath;
                }

                //获得要保存的文件路径
                string serverFileName = toFilePath + fileName;
                //物理完整路径                   
                string toFileFullPath = HttpContext.Current.Server.MapPath(toFilePath);

                //检查是否有该路径  没有就创建
                if (!Directory.Exists(toFileFullPath))
                {
                    Directory.CreateDirectory(toFileFullPath);
                }

                //将要保存的完整文件名               
                string toFile = toFileFullPath + fileName;

                ///创建WebClient实例      
                WebClient myWebClient = new WebClient();
                //设定windows网络安全认证   方法1
                myWebClient.Credentials = CredentialCache.DefaultCredentials;
                //设定windows网络安全认证   方法2
                //NetworkCredential cred = new NetworkCredential("SINOPANEL/xiangnan-xiao", "xiaoxiangnan");
                //CredentialCache cache = new CredentialCache();
                //cache.Add(new Uri("UploadPath"), "Basic", cred);
                //myWebClient.Credentials = cache;

                //要上传的文件      
                FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                //FileStream fs = OpenFile();      
                BinaryReader r = new BinaryReader(fs);
                //使用UploadFile方法可以用下面的格式      
                //myWebClient.UploadFile(toFile, "PUT",fileNamePath);      
                byte[] postArray = r.ReadBytes((int)fs.Length);
                Stream postStream = myWebClient.OpenWrite(toFile, "PUT");
                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                }
                else
                {
                    return "0|" + serverFileName + "|" + "文件目前不可写";
                }
                postStream.Close();


                return "1|" + serverFileName + "|" + "文件上传成功";
            }
            else
            {
                return "0|errorfile|" + "文件格式非法";
            }
        }
        catch (Exception e)
        {
            return "0|errorfile|" + "文件上传失败,错误原因:" + e.Message;
        }
    }

这是我用的ajax上传的  在本地可以   但是传到服务器上就不能上传了  提示未能找到文件,烦请各位大虾解救

问题补充: public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.Form["type"] == "上传") { string _fileNamePath = ""; try { _fileNamePath = context.Request.Form["upfile"]; //开始上传 string _savedFileResult = UpLoadFile(_fileNamePath); context.Response.Write(_savedFileResult); } catch { context.Response.Write("0|error|上传提交出错"); } } else { try { string Path = context.Request.Form["path"]; string _removeFileResult = RemoveFile(Path); context.Response.Write(_removeFileResult); } catch { context.Response.Write("0|error|删除提交出错"); } } } /// /// 上传文件 方法 /// /// /// public string UpLoadFile(string fileNamePath) { return UpLoadFile(fileNamePath, "/UpLoad/"); } public string RemoveFile(string Path) { try { string FilePath = HttpContext.Current.Server.MapPath(Path); FileInfo file = new FileInfo(FilePath); string filename = file.Name; file.Delete(); return "1|" + filename + "|" + "文件删除成功"; } catch (Exception e) { return "0|errorfile|" + "文件删除失败:" + e.Message; } } 我估计获取的不是用户本地上传的文件地址 而是服务器上的地址 求解
①尘不染的主页 ①尘不染 | 初学一级 | 园豆:11
提问于:2011-03-15 18:19
< >
分享
最佳答案
0
收获园豆:40
dotNetDR_ | 老鸟四级 |园豆:2058 | 2011-03-15 18:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册