• 大小: 3.81M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2020-12-14
  • 语言: C#
  • 标签: jQuery  Ajax  API  上传  文件  

资源简介

在Web API中使用jQuery AJAX实现文件上传的例子,传统的form表单提交会导致页面刷新,但是在有些情况下,我们不希望页面被刷新,这种时候我们都是使用Ajax的方式进行请求,将文件post后台api接口上传到服务器,后端开发者可以借鉴学习。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace TestFileUpload
{
    public class FileUploadController : ApiController
    {
        [HttpPost]
        public KeyValuePair UploadFile()
        {
            try
            {
                if (HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    // Get the uploaded image from the Files collection
                    var httpPostedFile = HttpContext.Current.Request.Files[“UploadedImage“];

                    if (httpPostedFile != null)
                    {
                        // Validate the uploaded image(optional)

                        // Get the complete file path
    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-08-28 23:18  TestFileUpload\packages\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.5.2.2\
     文件        3173  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.5.2.2\Microsoft.AspNet.WebApi.5.2.2.nupkg
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\
     文件      185032  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll
     文件      192626  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.xml
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\portable-wp8+netcore45+net45+wp81+wpa81\
     文件      150728  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\portable-wp8+netcore45+net45+wp81+wpa81\System.Net.Http.Formatting.dll
     文件      279955  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\portable-wp8+netcore45+net45+wp81+wpa81\System.Net.Http.Formatting.xml
     文件      188633  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Client.5.2.2\Microsoft.AspNet.WebApi.Client.5.2.2.nupkg
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\Content\
     文件         485  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\Content\web.config.transform
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\
     文件      471240  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.dll
     文件      539657  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.xml
     文件      239377  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.Core.5.2.2\Microsoft.AspNet.WebApi.Core.5.2.2.nupkg
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\net45\
     文件       82120  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\net45\System.Web.Http.WebHost.dll
     文件       10367  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\net45\System.Web.Http.WebHost.xml
     文件       42998  2014-08-28 23:18  TestFileUpload\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\Microsoft.AspNet.WebApi.WebHost.5.2.2.nupkg
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Newtonsoft.Json.6.0.4\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Newtonsoft.Json.6.0.4\lib\
     目录           0  2014-08-28 23:18  TestFileUpload\packages\Newtonsoft.Json.6.0.4\lib\net20\
     文件      493056  2014-08-28 23:18  TestFileUpload\packages\Newtonsoft.Json.6.0.4\lib\net20\Newtonsoft.Json.dll
     文件      516034  2014-08-28 23:18  TestFileUpload\packages\Newtonsoft.Json.6.0.4\lib\net20\Newtonsoft.Json.xml
............此处省略42个文件信息

评论

共有 条评论