weixin_33747129
码龄9年
关注
提问 私信
  • 博客:1,333,240
    社区:5
    问答:10,034
    1,343,279
    总访问量
  • 162
    原创
  • 1,872,891
    排名
  • 4,802
    粉丝
  • 37
    铁粉
IP属地以运营商信息为准,境内显示到省(区、市),境外显示到国家(地区)
IP 属地:江苏省
  • 加入CSDN时间: 2016-01-14
博客简介:

weixin_33747129的博客

查看详细资料
个人成就
  • 获得182次点赞
  • 内容获得18次评论
  • 获得1,113次收藏
  • 代码片获得197次分享
创作历程
  • 399篇
    2019年
  • 664篇
    2018年
  • 938篇
    2017年
  • 514篇
    2016年
  • 417篇
    2015年
  • 330篇
    2014年
  • 305篇
    2013年
  • 262篇
    2012年
  • 202篇
    2011年
  • 152篇
    2010年
  • 125篇
    2009年
  • 86篇
    2008年
  • 65篇
    2007年
  • 38篇
    2006年
  • 22篇
    2005年
  • 16篇
    2004年
成就勋章
创作活动更多

2024 博客之星年度评选报名已开启

博主的专属年度盛宴,一年仅有一次!MAC mini、大疆无人机、华为手表等精美奖品等你来拿!

去参加
  • 最近
  • 文章
  • 代码仓
  • 资源
  • 问答
  • 帖子
  • 视频
  • 课程
  • 关注/订阅/互动
  • 收藏
搜TA的内容
搜索 取消

处理从ajax发布的文件

答:

Below is my solution for downloading multiple files depending on some list which consists of some ids and looking up in database, files will be determined and ready for download - if those exist. I am calling C# MVC action for each file using Ajax.

And Yes, like others said, it is possible to do it in jQuery Ajax. I did it with Ajax success and I am always sending response 200.

So, this is the key:

  success: function (data, textStatus, xhr) {

And this is my code:

var i = 0;
var max = 0;
function DownloadMultipleFiles() {
            if ($(".dataTables_scrollBody>tr.selected").length > 0) {
                var list = [];
                showPreloader();
                $(".dataTables_scrollBody>tr.selected").each(function (e) {
                    var element = $(this);
                    var orderid = element.data("orderid");
                    var iscustom = element.data("iscustom");
                    var orderlineid = element.data("orderlineid");
                    var folderPath = "";
                    var fileName = "";

                    list.push({ orderId: orderid, isCustomOrderLine: iscustom, orderLineId: orderlineid, folderPath: folderPath, fileName: fileName });
                });
                i = 0;
                max = list.length;
                DownloadFile(list);
            }
        }

Then calling:

function DownloadFile(list) {
        $.ajax({
            url: '@Url.Action("OpenFile","OrderLines")',
            type: "post",
            data: list[i],
            xhrFields: {
                responseType: 'blob'
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("RequestVerificationToken",
                    $('input:hidden[name="__RequestVerificationToken"]').val());

            },
            success: function (data, textStatus, xhr) {
                // check for a filename
                var filename = "";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=
]*=((['"]).*?\2|[^;
]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                    var a = document.createElement('a');
                    var url = window.URL.createObjectURL(data);
                    a.href = url;
                    a.download = filename;
                    document.body.append(a);
                    a.click();
                    a.remove();
                    window.URL.revokeObjectURL(url);
                }
                else {
                    getErrorToastMessage("Production file for order line " + list[i].orderLineId + " does not exist");
                }
                i = i + 1;
                if (i < max) {
                    DownloadFile(list);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            },
            complete: function () {
                if(i===max)
                hidePreloader();
            }
        });
    }

C# MVC:

 [HttpPost]
 [ValidateAntiForgeryToken]
public IActionResult OpenFile(OrderLineSimpleModel model)
        {
            byte[] file = null;

            try
            {
                if (model != null)
                {
                    //code for getting file from api - part is missing here as not important for this example
                    file = apiHandler.Get<byte[]>(downloadApiUrl, token);

                    var contentDispositionHeader = new System.Net.Mime.ContentDisposition
                    {
                        Inline = true,
                        FileName = fileName
                    };
                    //    Response.Headers.Add("Content-Disposition", contentDispositionHeader.ToString() + "; attachment");
                    Response.Headers.Add("Content-Type", "application/pdf");
                    Response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
                    Response.Headers.Add("Content-Transfer-Encoding", "binary");
                    Response.Headers.Add("Content-Length", file.Length.ToString());

                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Error getting pdf", null);
                return Ok();
            }

            return File(file, System.Net.Mime.MediaTypeNames.Application.Pdf);
        }

As long as you return response 200, success in Ajax can work with it, you can check if file actually exist or not as the line below in this case would be false and you can inform user about that:

 if (disposition && disposition.indexOf('attachment') !== -1) {
回答问题 2020.04.01

jQuery异步/等待ajax调用

发布问题 2020.02.22 ·
0 回答

AJAX帖子编号作为字符串

发布问题 2019.11.06 ·
0 回答

通过Ajax将数据发送到API

答:

Your post function should look something like this:

$(".send-subscribe").click(function() {
    $.post("https://xxx/api/user/subscribe", {
        email : $("#send_email")
    }, function(data, status){
        console.log(status + " :: " + data);
    });
}); 

You than can use status and data to continue your work.

回答问题 2019.10.25

Palindrome Partitioning

Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return[ ["aa","b"], [...
转载
发布博客 2019.07.03 ·
998 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Pascal's Triangle II

Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Pascal's Triangle为给出行数,求所有结果,这题是给出任意行号,求该行的结果。Pascal's Triangle本身需要返回二维结果,可以在二维数组上直接操作。但是...
转载
发布博客 2019.07.03 ·
1006 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Learning Java language Fundamentals

Chapter 2 Learning Java language fundamentalsexercises:  1.What is Unicode?  Unicode is a computing industry standard for consistently encoding,representing,and handling text that's express...
转载
发布博客 2019.07.03 ·
1000 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

ValidationSummary控件

ValidationSummary控件用于在页面中的一处地方显示所有验证错误的列表。这个控件在使用大的表单时特别有用。如果用户在页面底部的表单字段中输入了错误的值,那么这个用户可能永远也看不到错误信息。不过,如果使用ValidationSummary控件,就可以始终在表单的顶端显示错误列表。你可能已经注意到每个验证控件都有ErrorMessage属性。我们原来不用ErrorMessage属性来...
转载
发布博客 2019.07.03 ·
2167 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

排列组合公式

注:排列与元素的顺序有关,组合与顺序无关.如231与213是两个排列,2+3+1的和与2+1+3的和是一个组合.1.排列及计算公式 从n个不同元素中,任取m(m≤n)个元素按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列;从n个不同元素中取出m(m≤n)个元素的所有排列的个数,叫做从n个不同元素中取出m个元素的排列数,用符号 p(n,m)表示. p(n...
转载
发布博客 2019.07.02 ·
3043 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

20050410:他们不过是一群政客

从来就没有把台湾人民的利益放在眼里,甚至不惜拿他们来做挡箭牌,一边还对日本谄媚有加。转载于:https://www.cnblogs.com/yidinghe/archive/2005/04/10/134987.html
转载
发布博客 2019.07.02 ·
292 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

数据特征分析——概述

一、6个基础分析思路:1、分布分析2、对比分析3、统计分析4、帕累托分析5、正态性检验6、相关性分析二、分布分析分布分析:研究数据的分布特征和分布类型,分定量数据、定性数据区分基本统计量。三、对比分析对比分析:两个互相联系的指标进行比较四、统计分析统计分析:统计指标对定量数据进行统计描述,常从集中趋势和离中趋势两个方面进行分析五、帕累托分析帕...
转载
发布博客 2019.07.02 ·
2529 阅读 ·
0 点赞 ·
0 评论 ·
7 收藏

Mybatis初始化机制

对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外。本章将通过以下几点详细介绍MyBatis的初始化过程。1.MyBatis的初始化做了什么 2. MyBatis基于XML配置文件创建Configuration对象的过程 3. 手动加载XML配置文件创建Configuration对象完成初始化,创建并使用SqlSessionFactory对象...
转载
发布博客 2019.07.01 ·
186 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Booksort 启发式函数很重要h(s1)<=h(s2)+cost(s1,s2);

Problem DescriptionThe Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next...
转载
发布博客 2019.06.30 ·
358 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Xstream序列化实体

Java序列化的日期为是很标准,XStream中转换为标准的做法import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import java....
转载
发布博客 2019.06.29 ·
236 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

算法的力量

算法的力量李开复真正学懂计算机的人(不只是“编程匠”)都对数学有相当的造诣,既能用科学家的严谨思维来求证,也能用工程师的务实手段来解决问题——而这种思维和手段的最佳演绎就是“算法”。虽然在摩尔定律的作用下,计算机的计算能力每年都在飞快增长,价格也在不断下降。可我们不要忘记,需要处理的信息量更是呈指数级的增长。应用程序的要求千变万化,很多时候需要把一个复杂...
转载
发布博客 2019.06.28 ·
243 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Tracking your habits in Org-mode

【纯属记录】 在org-mode中,你可以跟踪你的周期性事务或辅助培养习惯,比如每天阅读半小时,每天完成后org-mode会予以记录。如果你正计划每月培养一个好习惯,也可以使用这个功能来记录你的完成情况。 配置非常简单,参见官方文档:http://orgmode.org/manual/Tracking-your-habits.html#Tracking-your...
转载
发布博客 2019.06.27 ·
275 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog...

完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog。具有同时选择日期、时间的功能。在2.2、2.3平台,显示的效果可能会有一个大背景框在后面。4.0以上平台无此现象。此怪异效果通过各种手段未能解决。功能不影响正常使用。import java.util.Calendar;import androi...
转载
发布博客 2019.06.27 ·
280 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

个人代码库の自动粘合桌面边缘

using System.Windows.Forms;using System;namespace **{ public partial class ***: form { /* 【必要事件】: * No.1:窗体的 Move 事件。 * No.2:窗体的 MouseEnter事件。 * No.3:MouseLeave事...
转载
发布博客 2019.06.27 ·
198 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏

Python面向对象编程 - 一个记事本程序范例(二)

给程序加上控制台菜单menu.pyimport sysfrom notebook import Notebook, Noteclass Menu: '''Display a menu and respond to choices when run.''' def __init__(self): self.notebook = Noteb...
转载
发布博客 2019.06.27 ·
280 阅读 ·
0 点赞 ·
0 评论 ·
1 收藏

XMLHttpRequest - 原始AJAX初步

我们知道,传统的Web应用是request - response形式的,即浏览器向服务器发送请求,服务器进行处理,然后再对浏览器响应。这种形式最大的缺点就是:客户端需要等服务器处理完之后,向它响应才能看到结果,在此期间,用户只能等待。如果在网速很差的网络环境中,这是很很难忍受的。因此,AJAX技术应运而生。AJAX允许客户端和服务器能进行异步的交互。谈及AJAX,有个对象我们不得不...
转载
发布博客 2019.06.27 ·
245 阅读 ·
0 点赞 ·
0 评论 ·
0 收藏
加载更多