日期:2014-05-18 浏览次数:20465 次
IServiceProviderprovider=(IServiceProvider)HttpContext.Current; HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); byte[]bs=wr.GetPreloadedEntityBody(); if(!wr.IsEntireEntityBodyIsPreloaded()) { Int n=1024; byte[] bs2=newbyte[n]; while(wr.ReadEntityBody(bs2,n)>0) { } }
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); string uriString = @"D:\" + DateTime.Now.ToShortDateString(); string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);//文件名称+后缀 if (uriString.EndsWith(@"\") == false) { uriString = uriString + @"\"; } uriString = uriString + fileName; // 创建WebClient实例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); try { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString,"PUT",fileNamePath); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); } else { return "No Write."; } postStream.Close(); return "Succefull"; } catch (Exception ex) { return "Error:" + ex.Message; }