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 ...
随机推荐
- [转]Oracle中Hint深入理解
原文地址:http://czmmiao.iteye.com/blog/1478465 Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明 ...
- How Not to Crash #2: Mutation Exceptions 可变异常(不要枚举可变的集合)
How Not to Crash #2: Mutation Exceptions 可变异常html, body {overflow-x: initial !important;}html { font ...
- Mongo分片集群脚本
bash大法好啊,一键玩mongo. 我的mongo版本是:MongoDB shell version v4.0.2 这里准备为大家献上Mongo创建分片和可复制集的脚本,以及在部署的时候踩的坑. 分 ...
- Install CasperJS on Windows
Phantomjs installation additionsAppend ";C:\phantomjs" to your PATH environment variable. ...
- 代理ARP
代理ARP是ARP协议的一个变种. 对于没有配置缺省网关的计算机要和其他网络中的计算机实现通信,网关收到源计算机的 ARP 请求会使用自己的 MAC 地址与目标计算机的 IP地址对源计算机进行应答.代 ...
- android 中解析json格式数据
本文来自http://tonysun3544.iteye.com/category/188238 package com.tony.json; import android.app.Activity; ...
- python多线程生产消费
#!/usr/bin/env python# -*- coding: utf-8 -*- from threading import Threadfrom Queue import Queueimpo ...
- js json处理 双引号
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象 JSON字符串: var str1 = '{ "name": "cxh", ...
- Spring配置文件<context:property-placeholder>标签使用漫谈
<context:property-placeholder>标签提供了一种优雅的外在化参数配置的方式,不过该标签在Spring配置文件中只能存在一份!!! 众所周知,Spring容器是采用 ...
- PHP高级程序员必学
业务增长,给你的网站带来用户和流量,那随之机器负载就上去了,要不要做监控?要不要做负载均衡?用户复杂了,要不要做多终端兼容?要不要做CDN?数据量大了,要不要做分布?垂直分还是横向分?系统瓶颈在哪里? ...