期间编程没什么难度,,独一可能忽略导致功效掉败是asp.net mvc配置
对付压缩文件大的话,需要配置mvc的最大接收量:
<system.web> <httpRuntime maxRequestLength="2147483647" executionTimeout="3600" /> <!--允许上传数据巨细-> </system.web> <system.webServer> <!--允许上传文件长度,单位字节--> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483647"/> </requestFiltering> </security> </system.webServer>
winform中压缩和上传:
挪用部分如下:
string proPath = Application.StartupPath + "\\temp\\product\\" + project.ProjectName; ZipFile(proPath,proPath + ".zip");//压缩 var uploadResult = Upload_Request(WebConfig.UploadUrl + "/Mobile/ReceiveProjectFile", proPath + ".zip", project.ProjectName + ".zip");//上传 if (uploadResult == 0) { LoadingForm.CloseLoadingCircle(); Notification t = new Notification("方案附件上传掉败,请重试!", 3, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up, true, "error"); t.Show(); return; } File.Delete(proPath + ".zip");
/// <summary> /// 压缩文件 /// </summary> /// <param>待压缩文件路径</param> /// <param>压缩文件路径</param> public void ZipFile(string strFile, string strZip) { if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar; ZipOutputStream s = new ZipOutputStream(File.Create(strZip)); s.SetLevel(6); // 0 - store only to 9 - means best compression zip(strFile, s, strFile); s.Finish(); s.Close(); } private void zip(string strFile, ZipOutputStream s, string staticFile) { if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar; Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strFile); foreach (string file in filenames) { if (Directory.Exists(file)) { zip(file, s, staticFile); } else // 否则直接压缩文件 { //打开压缩文件 FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(tempfile); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } }
/// <summary> /// 将本地文件上传到指定的处事器(HttpWebRequest要领) /// </summary> /// <param>文件上传到的处事器</param> /// <param>要上传的本地文件(全路径)</param> /// <param>文件上传后的名称</param> /// <returns>告成返回1,掉败返回0</returns> private int Upload_Request(string address, string fileNamePath, string saveName) { int returnValue = 0; // 要上传的文件 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); //时间戳 string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n"); //请求头部信息 StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=‘"); sb.Append("file"); sb.Append("‘; filename="); sb.Append(saveName); sb.Append("‘"); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string strPostHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 按照uri创建HttpWebRequest东西 HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address); httpReq.Method = "POST"; //对发送的数据不使用缓存 httpReq.AllowWriteStreamBuffering = false; //设置获得响应的超不时间(300秒) httpReq.Timeout = 300000; httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary; long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length; long fileLength = fs.Length; httpReq.ContentLength = length; try { //每次上传4k int bufferLength = 4096; byte[] buffer = new byte[bufferLength]; //已上传的字节数 long offset = 0; //开始上传时间 DateTime startTime = DateTime.Now; int size = r.Read(buffer, 0, bufferLength); Stream postStream = httpReq.GetRequestStream(); //发送请求头部动静 postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); while (size > 0) { postStream.Write(buffer, 0, size); offset += size; Application.DoEvents(); size = r.Read(buffer, 0, bufferLength); } //添加尾部的时间戳 postStream.Write(boundaryBytes, 0, boundaryBytes.Length); postStream.Close(); //获取处事器真个响应 WebResponse webRespon = httpReq.GetResponse(); Stream s = webRespon.GetResponseStream(); StreamReader sr = new StreamReader(s); //读取处事器端返回的动静 String sReturnString = sr.ReadLine(); s.Close(); sr.Close(); if (sReturnString == "Success") { returnValue = 1; } else if (sReturnString == "Error") { returnValue = 0; } } catch { returnValue = 0; } finally { fs.Close(); r.Close(); } return returnValue; }
asp.net mvc接收压缩包,并解压: