Alex
Alex

Reputation: 683

Upload not working correctly, gives error message regarding file type

I have a web control and the backend code being used to upload files to a server (in a test environment, the folder of the solution)

The error messages that I'm using include "not DOCX" and "File size too big", and currently the control is not uploading the file and is only outputting "Is not DOCX".

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 == "document/docx")
                    {
                        //if the file is is less than 51mb
                        if (FileUploadControl.PostedFile.ContentLength < 2000)
                        {
                            //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("sitename.com", 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 2mb!";
                    }
                    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;
                }
            }
        }
    }
}

front of the web control..

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadFileControl.ascx.cs" Inherits="FileUpload.UploadFileControl" %>


<asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />

And my web form

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="FileUpload.test" %>

<%@ Register Src="~/UploadFileControl.ascx" TagPrefix="uc1" TagName="UploadFileControl" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="uploadForm" runat="server">
    <div>
        <uc1:UploadFileControl runat="server" ID="UploadFileControl" />
    </div>
    </form>
</body>
</html>

Upvotes: 0

Views: 963

Answers (3)

Amarnath Balasubramanian
Amarnath Balasubramanian

Reputation: 9460

The problem is with this line I guess

 if (FileUploadControl.PostedFile.ContentType == "document/docx")

Can you please refer the following link for different content type

http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm

Upvotes: 1

LittleSweetSeas
LittleSweetSeas

Reputation: 7074

It seems that DOCX content type is the following:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

You can see here a list of MS Office content types.

So, your code should be:

if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
   {
      //upload
   }
   else
      StatusLabel.Text = "Upload status: Only DOCX files are accepted!";

Upvotes: 1

Damith
Damith

Reputation: 63105

check for the file type by using extension of the file as below

if(Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower() ==".docx")
{
    // file type is docx.. do something 
}
else
    StatusLabel.Text = "Upload status: Only DOCX files are accepted!";

Upvotes: 1

Related Questions