我有一个控制器方法,它HttpPostedFileBase picture
作为参数。当然,它用于从表单加载图像。但现在我想使用该方法从另一种方法加载文件。我可以使用图像路径在代码中制作 HttpPostedFileBase 文件吗?或者可能是另一种解决方案是首选?
好的,代码:
public ActionResult UploadPicture(HttpPostedFileBase picture)
{
if (picture.ContentLength > Convert.ToInt32(ConfigurationManager.AppSettings["MaxMBFileSize"]) * 1024 * 1024 || picture.ContentLength == 0)
{
return ClientError(ErrorCodes.ClientErrorCodes.FileSizeError, "File size is incorrect");
}
else
{
string contentType = picture.ContentType;
if (!PictureHelper.ContentTypeIsValid(contentType))
{
return ClientError(ErrorCodes.ClientErrorCodes.MimeTypeError, "Incorrect file type");
}
else
{
string pictureName = Guid.NewGuid().ToString();
pictureName += picture.FileName.Substring(picture.FileName.LastIndexOf('.'));
string serverPath = AppDomain.CurrentDomain.BaseDirectory;
picture.SaveAs(serverPath + ConfigurationManager.AppSettings["LocalImagesPath"] + pictureName);
return Success(new { pictureName = pictureName });
}
}
}
真的,身体绝对不重要。当然我有类似的东西:
<form method="post" action="Photo\UploadPicture">
<input type="file">
<input type="submit" value="submit">
</form>
我想要类似的东西:
public ActionResult NewMethod()
{
string path = ""; // real path to file here
var file = OhMyGodMagicTransfer(path);
// sending request
request.attach(file);
request.send;
}