`
liuyanhui
  • 浏览: 37323 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
最近访客 更多访客>>
社区版块
存档分类
最新评论

asp.net不用FileUpload上传文件

    博客分类:
  • .NET
阅读更多
WebClient wc = new WebClient();
   wc.UploadFile("http://localhost:4953/AshxAllEvent/UpFileImage.ashx", "POST", result2[0].ToString());
//result2[0]文本本地绝对路径
//即C:\\Documents and Settings\\Administrator\\桌面\\Image\\热度趋势.png


UpFileImage.ashx:

            try
            {

                HttpFileCollection postedFile = context.Request.Files;
                if (postedFile.Count>0)
                {
                    string savepath = "";
                    string tempPath = "";
                    tempPath = System.Web.HttpContext.Current.Server.MapPath("..\\Temp\\");//System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
                    savepath = tempPath; //context.Server.MapPath(tempPath);
                    string filename = postedFile[0].FileName;
                    string sExtension = filename.Substring(filename.LastIndexOf('.'));
                    if (!Directory.Exists(savepath))
                        Directory.CreateDirectory(savepath);

                    string sNewFileName = filename.Substring(0,filename.IndexOf("."));
                    postedFile[0].SaveAs(savepath + @"\" + sNewFileName + sExtension);
                    context.Response.Write(tempPath + sNewFileName + sExtension);
                    context.Response.StatusCode = 200;
                }

            }
            catch (Exception ex)
            {
                context.Response.Write("Error: " + ex.Message);
            }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics