c#.net将图像文件上传到网站405错误

时间:2016-11-29 14:07:33

标签: c# asp.net-mvc visual-studio file-upload image-uploading

提出了类似的问题,我正试图在它们的基础上实施。但是,我无法上传图片文件到我的网站。我收到405错误。

这是我的代码:

//model
public class UploadModel
{
    public HttpPostedFileBase imageFile { get; set; }
}

//View
@Html.TextBoxFor(model => model.imageFile, new { type = "file", accept = "image/*" })

//Controller
[HttpPost]
public ActionResult UploadFile(UploadModel model)
{
    if (model.imageFile != null && model.imageFile.ContentLength > 0) //not null, gets right file
    {
        string web = "https://myWebSite.com/images/"; //without www
        string path = Path.Combine(Server.MapPath("~/Images/"), model.imageFile.FileName); 

        WebClient client = new WebClient();
        client.Credentials = CredentialCache.DefaultCredentials;
        byte[] responseArray = client.UploadFile(@web, path); //Here i get 405 error
        String response = System.Text.Encoding.ASCII.GetString(responseArray);
        client.Dispose();
    }
    return View();
}

当我调用WebClient.UploadFile方法时,我得到405错误。网址是正确的,网站不需要凭据。那我为什么会收到这个错误?

提前致谢。

0 个答案:

没有答案