广告
返回顶部
首页 > 资讯 > 精选 >AJAX如何实现图片预览与上传及生成缩略图的方法
  • 681
分享到

AJAX如何实现图片预览与上传及生成缩略图的方法

2023-06-08 08:06:03 681人浏览 八月长安
摘要

这篇文章将为大家详细讲解有关ajax如何实现图片预览与上传及生成缩略图的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。JS代码://ajax保存数据,后台方法里实现此方法 function&

这篇文章将为大家详细讲解有关ajax如何实现图片预览与上传及生成缩略图的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

JS代码:

//ajax保存数据,后台方法里实现此方法 function SaveData() {      filename = document.getElementById("idFile").value;     result =test_test_aspx.SaveData(filename).value;     if (result) {       alert("保存成功!");          }     return false;   }  //实现预览功能   function DrawImage(ImgD) {     var preW = 118;     var preH = 118;     var image = new Image();     image.src = ImgD.src;     if (image.width > 0 && image.height > 0) {       flag = true;       if (image.width / image.height >= preW/ preH) {         if (image.width > preW) {           ImgD.width = preW;           ImgD.height = (image.height * preW) / image.width;         }         else {           ImgD.width = image.width;           ImgD.height = image.height;         }         ImgD.alt = image.width + "x" + image.height;       }       else {         if (image.height > preH) {           ImgD.height = preH;           ImgD.width = (image.width * preH) / image.height;         }         else {           ImgD.width = image.width;           ImgD.height = image.height;         }         ImgD.alt = image.width + "x" + image.height;       }     }   } //当idFile内容改变时   function FileChange(Value) {     flag = false;     document.getElementById("showImg").style.display = "none";        document.getElementById("idImg").width = 10;     document.getElementById("idImg").height = 10;     document.getElementById("idImg").alt = "";     document.getElementById("idImg").src = Value;   }

以下为前台代码:

<div class="cbs"> <div class="l"><label>图片:</label></div> <div>   <input id="idFile" name="pic" type="file" runat="server" onchange="FileChange(this.value);" /> </div>     </div>      <div class="cbs"> <div class="l"><label>预览:</label></div> <div>   <img id="idImg" height="0" width="0" src="" alt="" onload="DrawImage(this);" /> //实现预览   <img id="showImg" width="118" height="118" alt="" runat="server" />  //加这个主要是为了实现查看时显示图片,因为上面的(idImg)加上runat="server" 报错,如有好的方法可以留言     </div> </div>

以下为AJAX方法:

[Ajax.AjaxMethod()] public bool SaveData(string fileNamePath) {   string serverFileName = "";   string sThumbFile = "";     string sSavePath = "~/Files/";   int intThumbWidth = 118;   int intThumbHeight = 118;   string sThumbExtension = "thumb_";   try   {  //获取要保存的文件信息  FileInfo file = new FileInfo(fileNamePath);  //获得文件扩展名  string fileNameExt = file.Extension;   //验证合法的文件  if (CheckFileExt(fileNameExt))  {    //生成将要保存的随机文件名    string fileName = GetFileName() + fileNameExt;    //检查保存的路径 是否有/结尾    if (sSavePath.EndsWith("/") == false) sSavePath = sSavePath + "/";     //按日期归类保存    string datePath = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd") + "/";    if (true)    {  sSavePath += datePath;    }    //获得要保存的文件路径    serverFileName = sSavePath + fileName;    //物理完整路径    string toFileFullPath = HttpContext.Current.Server.MapPath(sSavePath);     //检查是否有该路径 没有就创建    if (!Directory.Exists(toFileFullPath))    {  Directory.CreateDirectory(toFileFullPath);    }     //将要保存的完整文件名     string toFile = toFileFullPath + fileName;     ///创建WEBClient实例    WebClient myWebClient = new WebClient();    //设定windows网络安全认证     myWebClient.Credentials = CredentialCache.DefaultCredentials;       //要上传的文件    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);    }    postStream.Close();    //以上为原图    try    {  //原图加载    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(serverFileName)))  {    //原图宽度和高度     int width = sourceImage.Width;    int height = sourceImage.Height;    int smallWidth;    int smallHeight;     //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)     if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)    {   smallWidth = intThumbWidth;   smallHeight = intThumbWidth * height / width;    }    else    {   smallWidth = intThumbHeight * width / height;   smallHeight = intThumbHeight;    }     //判断缩略图在当前文件夹下是否同名称文件存在    int file_append = 0;    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(fileName) + fileNameExt;     while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))    {   file_append++;   sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(fileName) + file_append.ToString() + fileNameExt;    }    //缩略图保存的绝对路径     string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;     //新建一个图板,以最小等比例压缩大小绘制原图     using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))    {   //绘制中间图    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))   { //高清,平滑  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); g.DrawImage( sourceImage, new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel );   }   //新建一个图板,以缩略图大小绘制中间图    using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))   { //绘制缩略图  using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) {   //高清,平滑    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;   g.Clear(Color.Black);   int lwidth = (smallWidth - intThumbWidth) / 2;   int bheight = (smallHeight - intThumbHeight) / 2;   g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth,intThumbHeight, GraphicsUnit.Pixel);   g.Dispose();   bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFORMat.Jpeg);    return true;    }   }    }  }    }    catch    {  //出错则删除   System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(serverFileName));  return false;    }  }  else  {    return false;  }   }   catch (Exception e)   {  return false;   } } /// <summary> /// 检查是否为合法的上传文件 /// </summary> /// <param name="_fileExt"></param> /// <returns></returns> 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("yyyyMMddHHmmssff"));   serial.Append(rd.Next(0, 999999).ToString());   return serial.ToString();  }

什么是ajax

ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术,可以通过在后台与服务器进行少量数据交换,使网页实现异步更新。

关于“AJAX如何实现图片预览与上传及生成缩略图的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: AJAX如何实现图片预览与上传及生成缩略图的方法

本文链接: https://www.lsjlt.com/news/252706.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • AJAX如何实现图片预览与上传及生成缩略图的方法
    这篇文章将为大家详细讲解有关AJAX如何实现图片预览与上传及生成缩略图的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。JS代码://ajax保存数据,后台方法里实现此方法 function&...
    99+
    2023-06-08
  • 如何实现Ajax上传图片及上传前先预览功能
    这篇文章将为大家详细讲解有关如何实现Ajax上传图片及上传前先预览功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。上传之前的预览方式一先来说说图片上传之前的预览问题。这...
    99+
    2022-10-19
  • wordpress上传图片不自动生成缩略图的解决方法
    因为站点不需要显示这么多图片,只需要原图就OK了,所以就要修改下媒体设置。打开设置->媒体,将不需要的缩略图宽度高度都设置为0就可以了。如下图:但是这样设置之后,还是会生成其它分辨率的缩略图,笔者猜测应该是主题的问...
    99+
    2022-06-12
    wordpress上传 自动生成缩略图
  • vue.js如何实现图片上传预览及图片更换功能
    这篇文章将为大家详细讲解有关vue.js如何实现图片上传预览及图片更换功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果图:样式以及效果图一并展示1.HTML<...
    99+
    2022-10-19
  • React+ajax+java如何实现上传图片并预览功能
    小编给大家分享一下React+ajax+java如何实现上传图片并预览功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!之前有在网上找ajax上传图片的资料,大部...
    99+
    2023-06-08
  • C#图片处理如何生成缩略图的实现
    缩略图通常是将图片内容进行一定的缩小展现,或裁剪展现,主要有两个目的,一是提供一定的预览功能,二是节省屏幕展示空间、节省流量。在网站中我们通常运用在商品的列表,比如商城、图书、新闻等...
    99+
    2023-02-07
    C# 生成缩略图 C# 图片缩略图
  • Android如何实现图片生成卷角和圆角缩略图的方法
    这篇文章给大家分享的是有关Android如何实现图片生成卷角和圆角缩略图的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在 Android 的一些界面中,有时候我们需要为一副图片生成大小为 n * n 的缩略...
    99+
    2023-05-30
    android
  • Vue.js 2.0如何实现移动端拍照压缩图片预览及上传功能
    这篇文章主要介绍Vue.js 2.0如何实现移动端拍照压缩图片预览及上传功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先我来讲我实现这个拍照预览压缩上传的思路,准确的说应该是拍...
    99+
    2022-10-19
  • Java中如何实现实现文件资料上传并生成缩略图
    这篇文章主要介绍Java中如何实现实现文件资料上传并生成缩略图,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一:需求用户有一个需求就是收藏的功能,单纯的收藏记录好做,但是想加点难度就是,生成缩略图。类似B站的收藏一样...
    99+
    2023-06-22
  • SpringBoot 利用MultipartFile上传本地图片生成图片链接的实现方法
    方法一 实现类: public String fileUpload(MultipartFile file) { if(file == null){ retu...
    99+
    2022-11-12
  • Vue.js 2.0如何实现移动端拍照压缩图片上传预览功能
    小编给大家分享一下Vue.js 2.0如何实现移动端拍照压缩图片上传预览功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!在学习...
    99+
    2022-10-19
  • 如何使用H5实现上传本地图片以及预览功能
    这篇文章给大家分享的是有关如何使用H5实现上传本地图片以及预览功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。最近工作中需要H5上传显示图片的功能,如图:直接上代码:html部...
    99+
    2022-10-19
  • html5以及jQuery如何实现本地图片上传前的预览代码
    这篇文章主要为大家展示了html5以及jQuery如何实现本地图片上传前的预览代码,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带大家一起来研究并学习一下“html5以及jQuery如何实现本地图片上传前的预览代码”这篇文章...
    99+
    2023-06-06
  • 如何利用原生JS实现图片预览加上传(前后端交互)
    目录前言效果大致如下前端代码后端代码总结前言 最近在写vue项目的时候发现了个Vant的一个upload的图片上传的组件,就好奇了一下下,于是萌生了一个自己手写一个图片上传的组件的想...
    99+
    2022-11-13
  • 如何使用vue+axios+lrz.js实现微信端图片压缩上传方法
    这篇文章主要介绍如何使用vue+axios+lrz.js实现微信端图片压缩上传方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!业务场景微信端项目是基于Vux + Axios构建的,...
    99+
    2022-10-19
  • Vue3 使用v-md-editor如何动态上传图片的方法实现
    目录前端代码:后端的代码:前端代码: <v-md-editor :autofocus="true" v-model="blog.content" heig...
    99+
    2022-11-13
    Vue3动态上传图片 Vue3  v-md-editor动态上传
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作