基础知识,可由此衍生。原文: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. MFC工程说明readme

    ======================================================================== MICROSOFT FOUNDATION CLASS ...

  2. jieba user guide

    import sysimport jiebaimport jieba.analyseimport jieba.posseg as posg sentence=u'''深圳新闻网讯 10月30日,世界城 ...

  3. CentOS6.3升级Python到2.7.3版本

    http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python  -V  2 ...

  4. C语言 · 错误票据

    历届试题 错误票据   时间限制:1.0s   内存限制:256.0MB        锦囊1 排序. 锦囊2 把所有的编号排序,再扫描一遍数组,即可找到重复的和遗漏的.   问题描述 某涉密单位下发 ...

  5. C语言 · 选最大数

    算法提高 选最大数   时间限制:1.0s   内存限制:512.0MB      输入3个整数a.b.c,(数的范围是[1,10000])输出其中最大的数.(用指针实现) 样例输入 2 5 1 样例 ...

  6. CPP_运算符重载及友元

    运算符重载 两种重载方法1)成员函数 a + b => a.operator+(b); 一个参数 2)友元函数 a + b => operator+(a, b); 两个参数. friend ...

  7. 将安卓手机短信导入到iPhone6 plus中

    不越狱的情况下短信不能直接同步到iphone手机,视频.图片.联系人可以直接使用itools的手机搬家功能超方便从android到iphone中.短信得变通的处理才能导入. 工具: 安卓手机iPhon ...

  8. hive表增量抽取到oracle数据库的通用程序(二)

    hive表增量抽取到oracle数据库的通用程序(一) 前一篇介绍了java程序的如何编写.使用以及引用到的依赖包.这篇接着上一篇来介绍如何在oozie中使用该java程序. 在我的业务中,分为两段: ...

  9. hbase源码系列(八)从Snapshot恢复表

    在看这一章之前,建议大家先去看一下snapshot的使用.这一章是上一章snapshot的续集,上一章了讲了怎么做snapshot的原理,这一章就怎么从snapshot恢复表. restoreSnap ...

  10. 安卓程序代写 网上程序代写[原]自定义View

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...