上传不正常,不会给出正确的消息/文件太大/错误的文档类型

时间:2013-12-10 11:37:59

标签: c# asp.net .net upload controls

我的上传表单显示两条错误消息,即使我的文件是36kb并且它说它们超过100000,它仍然似乎无法正常工作。发生了什么?我正在从一个Web表单进行测试,当将控件放在服务器上时,它根本不输出更新的错误消息,而是输出旧消息。此外,它不是一次正确上传到文件结构(从事物的外观在同一文件夹内)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace FileUpload
{
    //this backend for the web control will be used to upload a file that will have it's XML tags pulled and displayed on a page. 
    //this code checks if the fileupload control has input inside it, then proceeds to the next page with the document saved.
    public partial class UploadFileControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //names the script manager which will be used when the user attempts to upload a form / gives an error if they incorrectly attempt to upload
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            //if file is located
            if (FileUploadControl.HasFile)
            {
                try
                {
                    //allow content type of document / docx
                    if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
                    {
                        //if the file is is less than 51mb
                        if (FileUploadControl.PostedFile.ContentLength < 10000)
                        {
                            //name the filename, find the path of the name
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            //path of server upload (we just need to save it as a variable to be found on the next page, as it will be made / deleted
                            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                            //update the label with file uploaded
                            StatusLabel.Text = "Upload status: File uploaded!";

                            //move onto template wizard page
                            Response.Redirect("http://portal.acoura.com/admin/templatewizard.aspx", false);

                            //will be used to grab the document string
                            return;

                        }
                        else
                            //display the size the file needs to be less than
                            StatusLabel.Text = "Upload status: The file has to be less than 10mb!";
                    }
                    else
                        //tell the user only docx files are accepted
                        StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
                }
                catch (Exception ex)
                {
                    //display the exception message, in which case it would be either size / type / if it's present
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您确定ContentLength系列吗?也许如果你改变这个

if (FileUploadControl.PostedFile.ContentLength < 10000)

到这个

if (FileUploadControl.PostedFile.ContentLength < 10485760) // 10mb

代码将成功运行