ajax文件上传找不到路径,jquery+ajax上传文件 提示未能映射路径。。

网上这例子有,就是Jquery+ajax的前台的JS部分就不贴出来了,发下后台的处理

namespace HNCE_AjaxUpFile{

///

/// $codebehindclassname$ 的摘要说明

///

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class ajaxUpFile : IHttpHandler    {

public void ProcessRequest(HttpContext context)        {                  context.Response.ContentType = "text/plain";

//验证上传的权限            //------功能暂留

string _fileNamePath = "";            try            {

_fileNamePath = context.Request.Form["upfile"];

//开始上传

string _savedFileResult = UpLoadFile(_fileNamePath);                context.Response.Write(_savedFileResult);

}            catch            {

context.Response.Write("0|error|上传提交出错");            }

}

///

/// 上传文件 方法

///

///

///

public string UpLoadFile(string fileNamePath)        {

return UpLoadFile(fileNamePath, "~/UpLoad/");

}

///

/// 上传文件 方法

///

///

///

/// 返回上传处理结果   格式说明: 0|file.jpg|msg   成功状态|文件名|消息    

public string UpLoadFile(string fileNamePath, string toFilePath)        {

try            {

//获取要保存的文件信息

FileInfo file = new FileInfo(fileNamePath);

//获得文件扩展名

string fileNameExt = file.Extension;

//验证合法的文件                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.Request.PhysicalApplicationPath+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("UserName", "UserPWD");                    //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;            }        }

///         /// 检查是否为合法的上传文件        ///         ///         ///         private bool CheckFileExt(string _fileExt)        {            string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg" };            for (int i = 0; i < allowExt.Length; i++)            {                if (allowExt[i] == _fileExt) { return true; }            }            return false;

}

public static string GetFileName()        {            Random rd = new Random();            StringBuilder serial = new StringBuilder();            serial.Append(DateTime.Now.ToString("yyyyMMddHHmmss"));            serial.Append(rd.Next(100000, 999999).ToString());            return serial.ToString();

}

public bool IsReusable        {

get            {                return false;            }        }    }}

就是红色那段出了问题,以应用程序运行没问题

我以网站的形式打开就有问题了。我们那项目就要用网站的形式开发。真不知道怎么办才好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值