Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]
基础知识,可由此衍生。原文: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#)[转]的更多相关文章
- 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 ...
- JAXB - XML Schema Types, Binary Data
Data that has no "natural" representation with printable characters must, for inclusion in ...
- 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 ...
- String or binary data would be truncated. The statement has been terminated.
常见的情况为:插入的值大于字段定义的最大长度. String or binary data would be truncated. The statement has been terminated
- String or binary data would be truncated
在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...
- 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 ...
- Bubble Babble Binary Data Encoding的简介以及bubblepy的安装使用方法
Bubble Babble Binary Data Encoding是由Antti Huima创建的一种编码方法,可以把二进制信息表示为由交替的元音和辅音组成的伪词(pseudo-words),主要用 ...
- 20180820 SQL 提示Error: String or binary data would be truncated
Error: String or binary data would be truncated,错误,是因为栏位给出的长度不够,增加初始化长度就可以了. 除了创建表的增加长度情况,还有一种是,SELE ...
- Interpret bytes as packed binary data
7.1. struct — Interpret bytes as packed binary data — Python 3.6.5 documentation https://docs.python ...
随机推荐
- 11月Android笔记
不知不觉又过了两个月了,过的够呛.新收获:百度云,视频直播,sqlite加密,lucene,SlidingPaneLayout. 我发现只要你有心,你期望的事情会接踵而来(不包括爱情= =) 上个游戏 ...
- C#2.0 Socket套接字编程之实例初探 200
首先从原理上解释一下采用Socket接口的网络通讯,这里以最常用的C/S模式作为范例,首先,服务端有一个进程(或多个进程)在指定的端口等待客户来连接,服务程序等待客户的连接信息,一旦连接上之后,就可以 ...
- android 中使用jwt token(json web token)--java
http://blog.csdn.net/mingzhnglei/article/details/51119836 下面贴上自己项目中的一个小小的example import com.nimbusds ...
- C# if为false仍然进入方法体,==和qeual结果不一致
场景: 代码: if( e.Key == Key.Tab) { // ... } 断点调试:结果为false,进入方法体 ??? 更改为: if(Key.Tab.Equals(e.key)) { ...
- 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型
今天写winform的时候遇到一个问题,提示: 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型, 主要是为了在子线程中更新UI线程,在wpf中同样的写法 ...
- [synergy]两台机器公用键盘鼠标
两台机器公用键盘鼠标 如果是Linux: 下载synergy相关的deb包,然后
- CPP_类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数
类默认函数:构造函数,拷贝构造函数,赋值函数和析构函数 // person.h #ifndef _PERSON_H_ #define _PERSON_H_ class Person{ public : ...
- 使用livereload实现自动刷新
livereload是一个web开发辅助工具,当我们修改完html.css和js的时候会自动刷新浏览器,解放码农的双手.这样在双屏切图.写js代码的时候会提高很多效率.livereload有很多版本, ...
- hive表增量抽取到oracle数据库的通用程序(二)
hive表增量抽取到oracle数据库的通用程序(一) 前一篇介绍了java程序的如何编写.使用以及引用到的依赖包.这篇接着上一篇来介绍如何在oozie中使用该java程序. 在我的业务中,分为两段: ...
- CentOS7安装google chrome浏览器
1,下载离线包 rpm 2,rpm -ivh ** 报错: lsb_release被chrome依赖 libXss.so被chrome依赖 libappindicator3被chrome依赖 yum ...