从我的WP8应用程序,我有一个按钮,发送文件到我的MVC WCF服务。问题是,我想要另一个按钮发送一个文件到服务,它对第二个文件做一些不同的事情。例如,我希望我的MVC是这样的:
[HttpPost]
public ActionResult Index()
{
foreach (string upload in Request.Files)
{
//do something with upload
}
}
[HttpPost]
public ActionResult Index()
{
foreach (string upload in Request.Files)
{
//do something with the second upload
}
}
我似乎不明白MVC如何区分分别上传的两个文件。它如何知道上传的文件将进入哪个ActionResult ?为了将上传的文件引导到预期的ActionResult,需要添加什么?
这将从我的WP8应用程序发送文件:
private async void UploadFile()
{
//byte[] picbyte = photoStream.ToArray();
//photoStream.Read(picbyte, 0, System.Convert.ToInt32(photoStream.Length));
//photoStream.Close();
//string connstr = @"Data Source=.SQLEXPRESS; Initial Catalog=ImageDB; Persist Security Info=True;User ID=sa";
//SqlConnection conn = new SqlConnection(connstr);
try
{
// Make sure there is a picture selected
if (photoStream != null)
{
// initialize the client
// need to make sure the server accepts network IP-based
// requests.
// ensure correct IP and correct port address
var fileUploadUrl = @"http://localhost:54931/fileupload";
var client = new HttpClient();
// Reset the photoStream position
// If you don't reset the position, the content lenght
// sent will be 0
photoStream.Position = 0;
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(photoStream), "file", fileName);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return
// a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
}
// Disable the Upload button
btnUpload.IsEnabled = false;
// reset the image control
imgSelectedImage.Source = null;
// Display the Uploaded message
txtMessage.Visibility = Visibility.Visible;
}
catch
{
// Display the Uploaded message
txtError.Visibility = Visibility.Visible;
}
}
如果你对你的文件做不同的事情,只是命名你的动作不同,然后你可以上传文件到不同的url,对于Index()它将是/,而对于DoSomethingElse()它将是/DoSomethingElse