首页 文章 精选 留言 我的

精选列表

搜索[mvc],共2761篇文章
优秀的个人博客,低调大师

Retrofit2.0+ .Net MVC4(WebApi) 上传多张图片

最近在做Android项目,使用的是MVP+Retrofit+rxjava+dagger的项目架构,RestFul 服务使用的.Net平台的WebApi。由于业务中需要有多文件上传的功能。所以在这里记载一下,便于以后查阅。 Android端Retrofit api 定义 在这次上传中,我们将实体类数据和文件数据一起上传到服务端。所以只写一个api就好 1 2 () Observable<HttpResult<String>>addEvent(()Evententity,()List<MultipartBody.Part>parts); 读取文件并将其转换成MuiltipartBody.Part 列表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 List<MultipartBody.Part>getFilesMap(){ =ArrayList<MultipartBody.Part>(); Filefile=; { (!=&&.size()>){ (Stringpath:){ file=File(path); RequestBodyrequestBody=RequestBody.(MediaType.(),file); MultipartBody.Partpart=MultipartBody.Part.(,file.getName(),requestBody); .add(part); } } }(Exceptione){ e.printStackTrace(); } ; } 调用Retrofit 进行网络请求,并将实体类数据和文件数据一并发送到 服务端 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 addEvent(){ Evententity=.getEntity(); List<MultipartBody.Part>fileMap=.getFilesMap(); .clear(); { Subscriptionsubscription=.getRetrofitInstance() .create(EventInterface.) .addEvent(entity,fileMap) .subscribeOn(.io()).observeOn(.ui()).subscribe(Subscriber<HttpResult<String>>(){ onCompleted(){ } onError(Throwablee){ .setLoadingIndicator(); e.printStackTrace(); } onNext(HttpResult<String>httpResult){ (httpResult.getResultCode()!=ConstData.HttpResult.){ .setLoadingIndicator(); }{ .setLoadingIndicator(); } } }); .add(subscription); }(Exceptione){ e.printStackTrace(); } } 在webapi 端对文件以及数据进行接收。由上面转化MultipartBody 那一个环节我们可以看到,传到服务端的数据是以HTTP形式传送的。数据类型都是multipart/form-data类型的。所以我们在服务端要使用http相关协议来进行接收。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [HttpPost] public HttpResult<string>AddEvent() { //获取EventET的属性 HttpRequestrequest=HttpContext.Current.Request; stringentity=request.Form[ 0 ]; BaseBNbaseBN= new BaseBN(); EventETet=baseBN.CTJsonSerializer<EventET>(entity); SystemBNbn= new SystemBN(); et.EVENT_ID=bn.CreateID( "EVENT" ,HaiQu.N, "EVENT_ID" , "EVENT" ,EventType.YiYou); List<string>addr= new List<string>(); //获取上传的文件 HttpFileCollectioncoll=request.Files; for ( int i= 0 ;i<coll.Count;i++) { HttpPostedFilefile=coll.Get(i); Stringfilename=file.FileName.Substring(file.FileName.LastIndexOf( "\\" )+ 1 ); if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath( "~/Upload/" +et.EVENT_ID))) //如果不存在就创建file文件夹 { Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath( "~/Upload/" +et.EVENT_ID)); } stringfileSavePath=string.Format( "~/Upload/" +et.EVENT_ID+ "/{0}" ,filename); file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(fileSavePath)); addr.Add(fileSavePath); } EventBNeventBN= new EventBN(); HttpResult<string>result=eventBN.AddEvent(et,addr,HaiQu.N); return result; } 这里有几点需要注意,实体类的数据,传输到服务端的时候,会以JSON字符串的形式传输到后台。这样的话,在后台使用的时候,需要对其进行反序列化。 另外,上传的文件,在HTTP中是以集合的形式存在,直接取出来,存储到本地就好。有一点需要注意的是,由于IIS对上传文件的限制,需要提前修改一下允许上传文件的上限。要不然,会上传不成功(这些都是已经踩过的坑)。 说在最后,其实上传文件很简单,网上有很多的大神已经讲解的非常详细。之所又自己啰嗦了一下,是因为,在做的过程中,踩了很多的坑,而且网上大多数人都只写了Android端,没有写服务端。再加上,.NET WebApi 用的人比较小众,所以就记录了一下。以后有时间会补上Spring 作为服务端的写法。以及Struts作为服务端的写法。 今天就到这里吧。 本文转自Eumenides_s 51CTO博客,原文链接:http://blog.51cto.com/xiaoshuaigege/1878634 ,如需转载请自行联系原作者

优秀的个人博客,低调大师

阿里云服务器的坑=====部署EF+MVC

异常处理汇总 ~ 修正果带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4599258.html 先参考:http://www.cnblogs.com/dunitian/p/4981572.html 网站部署好后==>继续404 网站日记如下: 2015-11-19 23:51:52,119 [15] ERROR log - System.InvalidOperationException: 未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=260882。 在 System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderServicesResolver.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 k) 在 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 在 System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) 在 System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) 在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) 在 System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) 在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) 在 System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key) 在 System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderServices(DbProviderFactory factory) System.Data.SqlClient ==>去项目中找没看见,估计就是不完全支持ef,或者dal层有问题,后来看见dal层有个EntityFramework.SqlServer.dll 发布的bin里面并没有,于是copy了一个,就可以了。。。。。 这个是阿里云木有的一些dll ok了~ www.dkill.net

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册