gpt4 book ai didi

c# - 当文件存在时,WebImage.GetImageFromRequest 返回 null

转载 作者:行者123 更新时间:2023-11-28 04:37:10 25 4
gpt4 key购买 nike

我的 WebImage.GetImageFromRequest 返回 null,即使存在文件也是如此。怎么会这样?

cshtml代码:

@{
if(IsPost)
{
if(Request["upload"] != null)
{
image = WebImage.GetImageFromRequest();
if(image != null)
{
// something
}
}

if(Request["btn"] != null)
{
// something
}
}
}
<form action="" method="post">
<table>
<tr>
<td>
<p>
Upload image:
</p>
</td>
<td>
<input type="file" name="img" />
<br/>
<input type="submit" name="upload" value="Upload" />
</td>
</tr>
</table>
<input type="submit" name="btn" value="update" />
</form>

最佳答案

尝试使用这种替代方法(因为存在一些错误):

public static WebImage GetImageFromRequest() {
var request = HttpContext.Current.Request;

if (request.Files.Length == 0) {
return null;
}

try {
var postedFile = request.Files[0];
var image = new WebImage(postedFile.InputStream) {
FileName = postedFile.FileName
};
return image;
} catch {
// The user uploaded a file that wasn't an image or an image format that we don't understand
return null;
}
}

来自 here

关于c# - 当文件存在时,WebImage.GetImageFromRequest 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16593366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com