• 启用 CORS 来解决这个问题(ajax跨域请求)


     1 <input type="file" name="btn_Upload" value="上传" id="btn_Upload" />
     2 <img src="" alt="" id="img_Upload" />
     3 
     4 @section Scripts
     5 {
     6     <link href="../../js/uploadifive-v1.2.2-standard/uploadifive.css" rel="stylesheet" />
     7     <script src="../../js/uploadifive-v1.2.2-standard/jquery.uploadifive.js"></script>
     8 }
     9 
    10 <script>
    11     $(function () {
    12         $('#btn_Upload').uploadifive({
    13             formData: { openId: '@ViewBag.OpenId' },
    14             multi: false,
    15             uploadScript: '@WXCommon.ConfigHelper.UploadUrl',
    16             onUploadComplete: function (file, data) {
    17                 if (data) {
    18                     var json = $.parseJSON(data);
    19                     if (json.res) {
    20                         alert(json.msg);
    21                         $('#img_Upload').attr({
    22                             src: '@WXCommon.ConfigHelper.ThumbnailPath' + '/' + '@ViewBag.OpenId' + '/' + json.fileName
    23                         });
    24                     } else {
    25                         alert(json.msg);
    26                     }
    27                 } else {
    28                     alert('服务器内部错误!');
    29                 }
    30             }
    31         });
    32     });
    33 </script>
    Javascript
     1 //独立部署的图片服务器
     2 public ActionResult Index()
     3 {
     4     //if (Request.HttpMethod == "OPTION")
     5     //{
     6     //    Response.AddHeader("Access-Control-Allow-Methods", "GET,POST");
     7     //    Response.AddHeader("Access-Control-Allow-Headers", "Content-Type,Accept");
     8     //    Response.AddHeader("Access-Control-Max-Age", "1728000");
     9     //    Response.AddHeader("Access-Control-Allow-Origin", "*");
    10     //}
    11     string openId = Request["openId"];
    12     HttpPostedFileBase httpPostedFileBase = Request.Files["Filedata"];
    13     string fileName = httpPostedFileBase.FileName;
    14     string extensionName = System.IO.Path.GetExtension(fileName).ToLower();
    15     if (extensionName == ".jpg" || extensionName == ".jpeg")
    16     {
    17         string virtualPath = "/uploads" + "/" + fileName.Replace(extensionName, "") + DateTime.Now.ToString("yyyyMMddhhmmss") + extensionName;
    18         string fullPath = Server.MapPath(virtualPath);
    19         httpPostedFileBase.SaveAs(fullPath);
    20 
    21         string savePath = Server.MapPath("/thumbnails" + "/" + openId);
    22         string thumbnailFileName = Thumbnail.GetThumbnailImage(160, 120, fullPath, savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    23         //删除原始文件
    24         //System.IO.File.Delete(fullPath);
    25         NowModel model = new NowModel();
    26         model.Id = 0;
    27         model.OpenId = openId;
    28         model.FileName = thumbnailFileName;
    29         nowBLL.Insert(model);
    30         return Json(new { res = true, msg = "上传成功!", fileName = thumbnailFileName });
    31     }
    32     return Json(new { res = false, msg = "上传失败!" });
    33 }
    ASP.Net
     1   <!--仅试用于IIS7以上版本,IIS6.0请用Response.AddHeader的方式-->
     2   <system.webServer>
     3     <validation validateIntegratedModeConfiguration="false" />
     4     <httpProtocol>
     5       <customHeaders>
     6         <add name="Access-Control-Allow-Origin" value="*"/>
     7         <add name="Access-Control-Allow-Methods" value="GET,POST,OPTION"/>
     8         <add name="Access-Control-Allow-Headers" value="Content-Type,soapaction"/>        
     9       </customHeaders>
    10     </httpProtocol>
    11   </system.webServer>
    图片服务器配置文件
    本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/tq1226112215/
    欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    webpack初识
    Vue+ElementUi项目实现表格-单行拖拽
    promise/async与await 的执行顺序梳理
    MDN社区
    angularjs中的异步操作
    javascript中的字符串和数组的互转
    angularjs的练习题
    angularjs基础知识
    开发的两种方式
    ASP.NET中的HttpClient发送请求
  • 原文地址:https://www.cnblogs.com/tq1226112215/p/4193999.html
Copyright © 2020-2023  润新知