利用ajaxupload怎么把图片上传到图片服务器上并显示 注:图片服务器与WEB服务器是分开的

沙伽more 2014-03-24 02:47:36
前台web是3台服务器做的集群 ,所有的图片都放在一个单独的服务器 web 上面 一共是 4个服务器
...全文
207 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
沙伽more 2014-03-25
  • 打赏
  • 举报
回复
引用 1 楼 wang136206441 的回复:
我先直接把我一般处理的代码贴出来吧, 现在这个只是支持本地上传:
 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            HttpPostedFile postedFile = context.Request.Files[0];
            string savePath = "/images/";//路径
            int filelength = postedFile.ContentLength;//文件大小
            int fileSize = 163600; //150K
            string fileName = "-1"; //返回的上传后的文件名
            
            if (filelength <= fileSize)
            {
                byte[] buffer = new byte[filelength];
                postedFile.InputStream.Read(buffer, 0, filelength);
                fileName = UploadImage(buffer, savePath, "jpg");
            }
            context.Response.Write(fileName);
        }

        private string UploadImage(byte[] imgBuffer, string uploadpath, string ext)
        {
            try
            {
                System.IO.MemoryStream m = new MemoryStream(imgBuffer);
 .....
        }
当然这个也是随便找的一个demo
还是不行啊,而且貌似ajaxupload还不支持IE6上传呢,有没有解决办法呢
沙伽more 2014-03-24
  • 打赏
  • 举报
回复
引用 6 楼 u014320300 的回复:
这是神马?
沙伽more 2014-03-24
  • 打赏
  • 举报
回复
引用 4 楼 grn0515 的回复:
汗!代码可能木有问题啊!你发布网站的时候,IIS绑定的主机头是相同的就可以了啊!
那如果出现域名不同的怎么办呢,也是一样吗?
grn0515 2014-03-24
  • 打赏
  • 举报
回复
汗!代码可能木有问题啊!你发布网站的时候,IIS绑定的主机头是相同的就可以了啊!
沙伽more 2014-03-24
  • 打赏
  • 举报
回复
引用 2 楼 grn0515 的回复:
服务器分开,但是域名是相同的啊!这和你在一台服务器上在不同磁盘是一个道理
但是我上面的那段代码好像只是支持,相对路径样;不能直接写绝对路径
grn0515 2014-03-24
  • 打赏
  • 举报
回复
服务器分开,但是域名是相同的啊!这和你在一台服务器上在不同磁盘是一个道理
沙伽more 2014-03-24
  • 打赏
  • 举报
回复
我先直接把我一般处理的代码贴出来吧, 现在这个只是支持本地上传:
 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            HttpPostedFile postedFile = context.Request.Files[0];
            string savePath = "/images/";//路径
            int filelength = postedFile.ContentLength;//文件大小
            int fileSize = 163600; //150K
            string fileName = "-1"; //返回的上传后的文件名
            
            if (filelength <= fileSize)
            {
                byte[] buffer = new byte[filelength];
                postedFile.InputStream.Read(buffer, 0, filelength);
                fileName = UploadImage(buffer, savePath, "jpg");
            }
            context.Response.Write(fileName);
        }

        private string UploadImage(byte[] imgBuffer, string uploadpath, string ext)
        {
            try
            {
                System.IO.MemoryStream m = new MemoryStream(imgBuffer);
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
                string imgname = CreateIDCode() + "." + ext;
                string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
                Image img = Image.FromStream(m);
                if (ext == "jpg")
                    img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
                else
                    img.Save(_path, System.Drawing.Imaging.ImageFormat.Gif);
                m.Close();
                return uploadpath + imgname;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string CreateIDCode()
        {
            DateTime Time1 = DateTime.Now.ToUniversalTime();
            DateTime Time2 = Convert.ToDateTime("1970-01-01");
            TimeSpan span = Time1 - Time2;   //span就是两个日期之间的差额   
            string t = span.TotalMilliseconds.ToString("0");

            return t;
        }
当然这个也是随便找的一个demo

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧