2013-12-09 71 views
0

我有一個網絡控制和後端代碼被用於將文件上傳到服務器(在測試環境中,該解決方案的文件夾)上傳已不能正常工作,使有關文件類型

錯誤消息的錯誤消息我正在使用的內容包括「不是DOCX」和「文件大小太大」,目前控件沒有上傳文件,而只輸出「不是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("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 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; 
       } 
      } 
     } 
    } 
} 

的Web控件前..

<%@ 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: " /> 

我的web表單

<%@ 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> 

回答

1

看來DOCX的內容類型是th e以下:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

您可以看到here MS Office內容類型列表。

所以,你的代碼應該是:

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

檢查的文件類型使用文件的擴展名,如下

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

我不會檢查文件擴展名,因爲它非常簡單,對於用戶來說,重命名文件擴展名和*破解*應用程序 – LittleSweetSeas