如何在 MVC C# 中实现第二个 HttpPost
How to implement a second HttpPost in MVC C#
在我的 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
在我的 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