日期:2014-05-18 浏览次数:20418 次
namespace AppGet { public class PushCommand : CommandBase { protected override void Execute() { string uri = string.Format("http://{0}/App/Push/{1}", ConfigurationManager.AppSettings["AppCenter"], Args["-file"]); Console.WriteLine(uri); HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; request.Method = "POST"; using (Stream stream = request.GetRequestStream()) { var buffer = new byte[4096]; using (FileStream fs = new FileStream(Args["-file"], FileMode.Open)) { while (true) { int r = fs.Read(buffer, 0, 4096); if (r <= 0) { break; } stream.Write(buffer, 0, r); } } } request.GetResponse(); } private const string DefaultContentType = "application/octet"; } }
------解决方案--------------------
前面copy的是csharp client upload的代码
下面是mvc中接受文件的代码
[AcceptVerbs(HttpVerbs.Post)] public void Push(string id) { string appName = id.Split('.')[0]; string targetFolder = Path.Combine(Server.MapPath("/Apps"), appName); if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } var buffer = new byte[4096]; using (FileStream fs = new FileStream(Path.Combine(targetFolder, id), FileMode.Create)) { while (true) { int r = Request.InputStream.Read(buffer, 0, 4096); if (r <= 0) { break; } fs.Write(buffer, 0, r); } } }
------解决方案--------------------
mvc中上传文件
[AcceptVerbs(HttpVerbs.Post)] public ContentResult Upload(HttpPostedFileBase FileData, string folder) { string result = ""; if (null != FileData) { try {