提出了类似的问题,我正试图在它们的基础上实施。但是,我无法上传图片文件到我的网站。我收到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错误。网址是正确的,网站不需要凭据。那我为什么会收到这个错误?
提前致谢。