简体   繁体   English

ASP.Net MVC文件上传不起作用

[英]ASP.Net MVC File Upload not working

Okay, so I was having a weird file upload problem. 好的,所以我遇到了奇怪的文件上传问题。 I had some code set up like this on the cshtml file. 我在cshtml文件中设置了一些类似的代码。

<form method="post" action="~/meme/createcustom2"  id="submitimage" name="submitimage">
        <div class="drag-box">
            <div class="drag">
                <div id="dragandrophandler">
                    <label class="hand">
                        Drag File or
                        Click to Upload&hellip;
                        <input type="file" id="uploadFile" name="uploadFile">                        </label>
                </div>
            </div>
            <span class="or">Or</span>
            <span class="uploadBtn" id="uploadBtn">
                <a href="#" class="btn">UPLOAD <i class="fa fa-upload" aria-hidden="true"></i></a>
            </span>
            <p>Choose an image from your computer</p>
        </div>

And no matter what I did, the file wasn't uploading. 而且无论我做什么,文件都不会上传。 I'd see the entry in the Forms collection, but no entry in the Files collection. 我会在Forms集合中看到该条目,但在Files集合中看不到任何条目。 Well, that vexed me this entire morning. 好吧,这使我整个早晨烦恼。

What am I missing? 我想念什么?

There are a lot of questions like this, and most answers suggesting changing the input tag's name attribute, that might help in some cases, but this is not why the problem occurs. 这样的问题很多,大多数答案建议更改输入标签的name属性,这在某些情况下可能会有所帮助,但这不是为什么会出现此问题。

I could find it after a little hunting. 经过一番狩猎,我可以找到它。

My form tag is missing the following attribute 我的表单标签缺少以下属性

enctype="multipart/form-data"

This attribute is needed before the form will accept binary files. 在表单接受二进制文件之前,需要此属性。 Otherwise it only accepts uuencoded text data. 否则,它仅接受uuencoded文本数据。

I put that in, and changed my form tag to look like this 我把它放进去,然后改变了我的表单标签,看起来像这样

<form method="post" action="~/meme/createcustom2" enctype="multipart/form-data" id="submitimage" name="submitimage">

That solved the problem. 那解决了问题。

Hope it helps someone. 希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM