基础知识,可由此衍生。原文: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. C#学习笔记(6)——大项目增删改查

    说明(2017-5-28 11:35:39): 1. 需要注意的地方很多 2. 首先是连接字符串str要直接写在类里面,不然每个按钮里面都要写一遍. 3. 查询用到sqlDataReader等三个方法 ...

  2. Eigen教程(9)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是 ...

  3. phpstorm设置断点过程

    1.为php安装xdebug,方法在上一篇中有详细介绍 2.注意这个时候需要修改php.ini内容如下: [Xdebug]zend_extension_ts ="d:/wamp/php/ex ...

  4. duubo开发时直连(不需要注册中心)

    前言 在dubbo开发时,一般开发人员不连注册中心,在这里记录下1.consumer端配置 <?xml version="1.0" encoding="UTF-8& ...

  5. open source Swift, Objective-C and the next 20 years of development

    Q&AApple's Craig Federighi talks open source Swift, Objective-C and the next 20 years of develop ...

  6. Linux下高并发socket最大连接数所受的各种限制(转)

    1.修改用户进程可打开文件数限制在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个 ...

  7. HTML5游戏引擎排行榜

    个好的游戏引擎,能够大大简化游戏的开发实现,html5gameengine.com网站对国内外所有HTML5游戏引擎进行了排名,排名不仅给出了价格.流行度.分数以及最新发布时间,通过点击引擎名称可以了 ...

  8. centos5中添加163yum源

    1.我们用linux系统的时候,经常需要使用yum.但是如果使用系统自带的yum源时,往往速度很慢,而且经常容易出错.其实国内有很多优秀的开源的yum源.比如(163,sohu,中科大).这里我以16 ...

  9. 手动释放linux内存cache

    总有很多朋友对于Linux的内存管理有疑问,之前一篇linux下的内存管理方式似乎也没能清除大家的疑虑.而在新版核心中,似乎对这个问题提供了新的解决方法,特转出来给大家参考一下.最后,还附上我对这方法 ...

  10. C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute

    需要反射的DLL里的一个类: namespace ElegantWM.WebUI.Areas.Admin.Controllers { [Description("功能模块管理")] ...