急:一个ajax上传的问题


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上传的  在本地可以   但是传到服务器上就不能上传了  提示未能找到文件,烦请各位大虾解救

Helenr
浏览 302回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP