flex上传文件到wcf restful

wcf restful 代码
 /// 
        /// 上传文件
        /// 
        /// post文件流
        /// 提交时附带信息。本例子中当文件名用
        /// 
        [WebInvoke(UriTemplate = "Add/{file}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public SampleItem AddFile(Stream stream, string file)
        {
            //获取web的上下文
            var ctx = WebOperationContext.Current;

            //判断附带信息。没有信息返回错误信息
            if (!string.IsNullOrEmpty(file))
            {
                //获得服务器保存文件的文件夹路径
                string folder = System.Web.Hosting.HostingEnvironment.MapPath("~/Files");
                //判断文件夹是否存在,不在时新建
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                //获得随机数
                System.Random objRand = new Random();
                //获得当前日期
                System.DateTime date = DateTime.Now;
                //生成随机文件名
              string  saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
                //拼接出随进文件名
                 string  fileName = file + saveName;
                //拼接路径和文件名
                 string path = Path.Combine(folder, fileName);
               
                //保存文件
                    using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        //获得上传文件的大小
                        long ByteLength = WebOperationContext.Current.IncomingRequest.ContentLength;
                        //生成数组
                        byte[] fileContent = new byte[ByteLength];
                        //读取文件流
                        stream.Read(fileContent, 0, fileContent.Length);
                        //文件写入
                        fs.Write(fileContent, 0, fileContent.Length);
                        //清空文件流换成
                        fs.Flush();
                       // ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                        //返回信息
                        return new SampleItem() { Id = 11 , StringValue ="true"};
                    }

                 
            }
            else
            {
              //  ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Accepted;
                //返回错误信息
                return new SampleItem() { Id = 22, StringValue = "false" };
            }
        
        }



flex 代码



	
		
	
	
	
		
	
	
	
	
		
	
	
	
	
		
		
		 
		
	
	
		
	



嵌入页面代码




 
    
    
        
                 
        
        
     
    
    

           
                
                
                
                
                  
                
                
                
                    
                    
                    
                      
                    
                
                
                    

Either scripts and active content are not permitted to run or Adobe Flash Player version 11.1.0 or greater is not installed.

Get Adobe Flash Player


你可能感兴趣的:(wcf,restful,api)