基础知识,可由此衍生。原文:http://uniapple.net/blog/?p=2050

In this post, I will show you how to upload a file using Ajax (Asynchronous JavaScript and XML) and receive the binary data in Asp.net C#. And it is ultra simple!

1. HTML

<input id="bannerImage" name="bannerImage" type="file"><input onclick="javascript:uploadfile()" type="button" value="upload">

2. JS

function uploadfile(){
var bannerImage = $("#bannerImage").val(); if (bannerImage) {
var file = document.getElementById('bannerImage').files[0];
var formData = new FormData();
formData.append(file.name, file); var xhr = new XMLHttpRequest();
var url = "/Category/UpdateBannerImage";
xhr.open('POST', url, true);
xhr.onload = function (e) {
var response = $.parseJSON(e.target.response);
console.log(response.fileName);
}; xhr.send(formData); // multipart/form-data }
}

(Sending Binary Data)

3. C#, Receiving binary data

public JsonResult UpdateBannerImage()
{
HttpPostedFileBase bannerImage = Request.Files[0] as HttpPostedFileBase;
if (bannerImage != null && bannerImage.ContentLength > 0)
{
var fileName = Path.GetFileName(bannerImage.FileName);
fileName = Regex.Replace(fileName, @"\s|\$|\#\%", "");
var path = Path.Combine(Server.MapPath("~/images/category_banners"), fileName);
bannerImage.SaveAs(path); return Json(new { success = false ,error = false, message = "Image has been updated successfully", fileName = fileName });
}
else
{
return Json(new { success = true, error = "File is empty" });
}
}

Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]的更多相关文章

  1. jQuery AJAX Call for posting data to ASP.Net page ( not Get but POST)

    the following jQuery AJAX call to an ASP.Net page. $.ajax({ async: true, type: "POST", url ...

  2. JAXB - XML Schema Types, Binary Data

    Data that has no "natural" representation with printable characters must, for inclusion in ...

  3. The state of binary data in the browser

    The state of binary data in the browser Or: "So you wanna store a Blob, huh?" TL;DR Don't ...

  4. String or binary data would be truncated. The statement has been terminated.

    常见的情况为:插入的值大于字段定义的最大长度. String or binary data would be truncated. The statement has been terminated

  5. String or binary data would be truncated

    在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...

  6. Server Job: error: String or binary data would be truncated. The statement has been terminated.

    "String or binary data would be truncated. The statement has been terminated" most probabl ...

  7. Bubble Babble Binary Data Encoding的简介以及bubblepy的安装使用方法

    Bubble Babble Binary Data Encoding是由Antti Huima创建的一种编码方法,可以把二进制信息表示为由交替的元音和辅音组成的伪词(pseudo-words),主要用 ...

  8. 20180820 SQL 提示Error: String or binary data would be truncated

    Error: String or binary data would be truncated,错误,是因为栏位给出的长度不够,增加初始化长度就可以了. 除了创建表的增加长度情况,还有一种是,SELE ...

  9. Interpret bytes as packed binary data

    7.1. struct — Interpret bytes as packed binary data — Python 3.6.5 documentation https://docs.python ...

随机推荐

  1. Android——简易计算器(转)

    这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了.现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多.采用的是相对布局 ...

  2. JVM垃圾回收算法(最全)

    JVM垃圾回收算法(最全) 下面是JVM虚拟机运行时的内存模型: 1.方法区 Perm(永久代.非堆) 2.虚拟机栈 3.本地方法栈 (Native方法) 4.堆 5.程序计数器 1 首先的问题是:j ...

  3. SQL SERVER 事务和锁

    内容皆整理自网络 一.事务 作者:郭无心链接:https://www.zhihu.com/question/31346392/answer/59815366来源:知乎著作权归作者所有.商业转载请联系作 ...

  4. android开发(35) fragment和actionbar组合使用。解决不触发onOptionsItemSelected的问题,获得actionbar 的默认 get icon

    先说说我的使用场景: 我写了一个activity,使用了actionbar. 在这个activity中,有fragment,默认先打开一个 homeFragment,点击某个按钮会进入 detailF ...

  5. 【C】——setvbuf(scanf内存溢出问题)

    下面设置了一个长度为20的缓存区,循环对value进行赋值并输出: #include<stdio.h> #define BUFSIZE 20 int main(int argc, char ...

  6. SpringMVC接受JSON参数详解及常见错误总结

    SpringMVC接受JSON参数详解及常见错误总结 SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一 ...

  7. jQuery imgAreaSelect Examples

    案例:前端图片截取功能 分布说明A:选择File本地选择的图片 B:根据需求按比例缩放图片 C:区域选择型操作 A: 选择图片 <input class="upfile" t ...

  8. 关于GO语言遇到illegal UTF-8 encoding 随手记录

    在使用汉字的时候会报错 解决方案 editpad++ 修改编码为UTF-8 保存就可以了~bingo

  9. Axure RP Pro 7.0 注册码

    用户名:aaa注册码:h624pifAqt7It5e8boKkML+Y4RjDX5xknP4k7QktJYQoxsvv7VUS7hBCv/2ef45P

  10. C/C++ 查看数组类型长短 , python的len的封装原理

    sizeof什么时间加括号什么时候不加括号? sizeof当用应用于一个表达式的时候不需要圆括号: 例如 sizeof  i; 如果运算符优先的时候需要: 例如sizeof(i+j); 方法一. in ...