基础知识,可由此衍生。原文: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

  1. <input id="bannerImage" name="bannerImage" type="file"><input onclick="javascript:uploadfile()" type="button" value="upload">

2. JS

  1. function uploadfile(){
  2. var bannerImage = $("#bannerImage").val();
  3.  
  4. if (bannerImage) {
  5. var file = document.getElementById('bannerImage').files[0];
  6. var formData = new FormData();
  7. formData.append(file.name, file);
  8.  
  9. var xhr = new XMLHttpRequest();
  10. var url = "/Category/UpdateBannerImage";
  11. xhr.open('POST', url, true);
  12. xhr.onload = function (e) {
  13. var response = $.parseJSON(e.target.response);
  14. console.log(response.fileName);
  15. };
  16.  
  17. xhr.send(formData); // multipart/form-data
  18.  
  19. }
  20. }

(Sending Binary Data)

3. C#, Receiving binary data

  1. public JsonResult UpdateBannerImage()
  2. {
  3. HttpPostedFileBase bannerImage = Request.Files[0] as HttpPostedFileBase;
  4. if (bannerImage != null && bannerImage.ContentLength > 0)
  5. {
  6. var fileName = Path.GetFileName(bannerImage.FileName);
  7. fileName = Regex.Replace(fileName, @"\s|\$|\#\%", "");
  8. var path = Path.Combine(Server.MapPath("~/images/category_banners"), fileName);
  9. bannerImage.SaveAs(path);
  10.  
  11. return Json(new { success = false ,error = false, message = "Image has been updated successfully", fileName = fileName });
  12. }
  13. else
  14. {
  15. return Json(new { success = true, error = "File is empty" });
  16. }
  17. }

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. 基于jQuery自适应宽度跟高度可自定义焦点图

    基于jQuery自适应宽度跟高度可自定义焦点图.这是一款带左右箭头,缩略小图切换的jQuery相册代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <section cl ...

  2. Android——BroadcastReceiver

    注释:一般广播不会被阻断,有序广播则会被阻断 注释:这是用动态注册的广播,必须要解绑 xml <?xml version="1.0" encoding="utf-8 ...

  3. Android——简易计算器(转)

    这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了.现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多.采用的是相对布局 ...

  4. Nginx+php (十六)

    [教程主题]:Nginx+php [课程录制]: 创E [主要内容] [1] 编译PHP 初始环境: 为了省事把所需要的库文件全都安装上,可以使用rpm包安装,也可以用yum命令安装, yum -y  ...

  5. 【jquery】图片前后对比效果——beforeAfter

    今天分享一款 jquery 插件——图片前后对比(beforeAfter),效果如下: 使用方法: <!DOCTYPE HTML> <html lang="en" ...

  6. 再谈git的http服务

    因为git服务器搬迁,需要重新安装git服务器,在网上搜索了下,发现之前的方法太复杂,复杂到自己都没彻底弄明白.其实通过git自带的git-http-backend脚本配合apache2的http服务 ...

  7. js学习(一)-动态添加、修改、删除对象的属性和方法

    //-----------------------js代码--------------------------- function class1(){ } //-------------------- ...

  8. Navi.Soft31.WebMVC框架(含示例地址)

    1概述 1.1应用场景 互联网高速发展,互联网软件也随之越来越多,Web程序越来越被广泛使用.它部署简单,维护方便,深得众多软件公司使用 Bootstrap前端框架,是最近非常流行的框架之一.它简洁, ...

  9. 微信中关闭网页输入内容时的安全提示 [干掉 “防盗号或诈骗,请不要输入QQ密码”]

    未设置之前: 需要把域名加入白名单 设置方法:微信公共平台后台-->公众号设置--->功能设置--->填写业务域名即可.

  10. Html5學習重點清單

    SVG webSQL 數據庫 SSE 服務推送 MathML 基於xml語法 Web 存储 webSockets通信 canvas 畫布操作 音頻和視頻 地理位置 Geolocation API We ...