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 ...
随机推荐
- 基于jQuery自适应宽度跟高度可自定义焦点图
基于jQuery自适应宽度跟高度可自定义焦点图.这是一款带左右箭头,缩略小图切换的jQuery相册代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <section cl ...
- Android——BroadcastReceiver
注释:一般广播不会被阻断,有序广播则会被阻断 注释:这是用动态注册的广播,必须要解绑 xml <?xml version="1.0" encoding="utf-8 ...
- Android——简易计算器(转)
这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了.现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多.采用的是相对布局 ...
- Nginx+php (十六)
[教程主题]:Nginx+php [课程录制]: 创E [主要内容] [1] 编译PHP 初始环境: 为了省事把所需要的库文件全都安装上,可以使用rpm包安装,也可以用yum命令安装, yum -y ...
- 【jquery】图片前后对比效果——beforeAfter
今天分享一款 jquery 插件——图片前后对比(beforeAfter),效果如下: 使用方法: <!DOCTYPE HTML> <html lang="en" ...
- 再谈git的http服务
因为git服务器搬迁,需要重新安装git服务器,在网上搜索了下,发现之前的方法太复杂,复杂到自己都没彻底弄明白.其实通过git自带的git-http-backend脚本配合apache2的http服务 ...
- js学习(一)-动态添加、修改、删除对象的属性和方法
//-----------------------js代码--------------------------- function class1(){ } //-------------------- ...
- Navi.Soft31.WebMVC框架(含示例地址)
1概述 1.1应用场景 互联网高速发展,互联网软件也随之越来越多,Web程序越来越被广泛使用.它部署简单,维护方便,深得众多软件公司使用 Bootstrap前端框架,是最近非常流行的框架之一.它简洁, ...
- 微信中关闭网页输入内容时的安全提示 [干掉 “防盗号或诈骗,请不要输入QQ密码”]
未设置之前: 需要把域名加入白名单 设置方法:微信公共平台后台-->公众号设置--->功能设置--->填写业务域名即可.
- Html5學習重點清單
SVG webSQL 數據庫 SSE 服務推送 MathML 基於xml語法 Web 存储 webSockets通信 canvas 畫布操作 音頻和視頻 地理位置 Geolocation API We ...