【问题标题】:How to implement a second HttpPost in MVC C#如何在 MVC C# 中实现第二个 HttpPost
【发布时间】:2015-07-07 20:12:03
【问题描述】:

在我的 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 中使用 controlleraction
  • 这是从发送它的应用程序完成的吗?为了以防万一,我已将其添加到问题中。
  • 假设fileupload是你的控制器,然后调用localhost:54931/fileupload/<your method name>

标签: c# asp.net-mvc wcf http-post


【解决方案1】:

如果您对文件执行不同的操作,只需以不同的方式命名您的操作,然后您可以将文件上传到不同的 url,对于 Index() 它将是 /,对于另一个说 DoSomethingElse() 它'将是 /DoSomethingElse

【讨论】:

  • 我想知道索引。感谢您的明确答复。
  • 乐于助人:),如果你喜欢我的回答,请点赞,谢谢
猜你喜欢
  • 1970-01-01
  • 2020-12-01
  • 2016-06-23
  • 2023-01-24
  • 2012-09-07
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多