2016-01-20 75 views
0

我有一个MVC应用程序(例如MVCApp1),我通过上传控件将文件(例如example.dll)上传到我的MVCApp1。我需要我的example.dll存储到另一个应用程序的bin文件夹(如WebAPIApp)从MVC应用程序上传文件并存储在Web API Bin文件夹

我怎么能做到这一点,我应该写在

[HttpPost] 
public async Task<ActionResult> FileUpload(HttpPostedFileBase file) 
{ 
} 

回答

0

最后我找到了我的答案,可能是有用ü

=======的Web API代码================

公共类UploadController:ApiController {

public async Task<HttpResponseMessage> Post() 
    { 
     // Check whether the POST operation is MultiPart? 
     if (!Request.Content.IsMimeMultipartContent()) 
     { 
      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
     } 
// Prepare CustomMultipartFormDataStreamProvider in which our multipart form 
     // data will be loaded. 
     string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data"); 
     CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation); 
     List<string> files = new List<string>(); 

     try 
     { 
      // Read all contents of multipart message into CustomMultipartFormDataStreamProvider. 
      await Request.Content.ReadAsMultipartAsync(provider); 

      foreach (MultipartFileData file in provider.FileData) 
      { 
       files.Add(Path.GetFileName(file.LocalFileName)); 
      } 

      // Send OK Response along with saved file names to the client. 
      return Request.CreateResponse(HttpStatusCode.OK, files); 
     } 
     catch (System.Exception e) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
     } 
    } 
} 

// We implement MultipartFormDataStreamProvider to override the filename of File which 
// will be stored on server, or else the default name will be of the format like Body- 
// Part_{GUID}. In the following implementation we simply get the FileName from 
// ContentDisposition Header of the Request Body. 
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider 
{ 
    public CustomMultipartFormDataStreamProvider(string path) : base(path) { } 

    public override string GetLocalFileName(HttpContentHeaders headers) 
    { 
     return headers.ContentDisposition.FileName.Replace("\"", string.Empty); 
    } 
} 

========================控制台应用程序代码===============

class Program 
{ 
    static void Main(string[] args) 
    { 
     using (var client = new HttpClient()) 
     using (var content = new MultipartFormDataContent()) 
     { 
      // Make sure to change API address 
      client.BaseAddress = new Uri("http://localhost:53798/"); 


      // Add first file content 
      var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"c:\Users\aisadmin\Desktop\Me\NF2202533167366.pdf")); 
      fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
      { 
       FileName = "Sample.pdf" 
      }; 

      // Add Second file content 
      var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"c:\Users\aisadmin\Desktop\Sample.txt")); 
      fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
      { 
       FileName = "Sample.txt" 
      }; 

      content.Add(fileContent1); 
      content.Add(fileContent2); 

      // Make a call to Web API 
      var result = client.PostAsync("/api/upload", content).Result; 

      Console.WriteLine(result.StatusCode); 
      Console.ReadLine(); 
     } 
    } 
} 

enter image description here

相关问题