日期:2014-05-17 浏览次数:20414 次
<input id="file" style="width: 300px" onchange="tree_showpic()" type="file" /> ajax执行异步上传 var content="path="+tree_$("file").value; dosavemymy("loadpic.aspx",content,"uploadpic");
Page_Load调用上传函数 string loadmes = UpLoadFile(Request.Form["path"]); Response.Write(loadmes); Response.End(); return; 上传函数 private string UpLoadFile(string fileNamePath) { //获取上传图片格式 string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1).ToLower(); if ("jpg|gif|png|bmp".IndexOf(fileNameExt) >= 0) { //根据用户ID指定头像存储地址 int userid = Int32.Parse(Request.Cookies["mymyid"].Value.ToString()); string mainpath = Server.MapPath("../"); string savepath1 = mainpath + "mymyfiles\\" + ((int)(userid / 1000)).ToString() + "\\" + ((int)(userid / 100)).ToString() + "\\" + userid.ToString() + "\\" + "headpic1.jpg"; //创建WebClient实例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); try { byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(savepath1, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); postStream.Close(); return "恭喜您,文件上传成功!"; } else { postStream.Close(); return "文件目前不可写!"; } } catch(Exception e) { return "对不起上传出错,请稍后再试。错误信息:"+e.Message.ToString(); } } else return "上传的文件格式不对,必须为GIF、JPG、PNG和BMP中的一种!"; }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>上传</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/jquery-1.2