C#TSQL Mutilple HasFile

时间:2016-01-06 12:23:00

标签: c# asp.net

我在使用C#和ASP.NET,TSQL开发应用程序时遵循本指南。 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile%28v=vs.110%29.aspx

我想知道如何调整这一点,以便用户有多个单独的框用于上传。我这样做的原因是因为每个文件上传旁边都有一个复选框,其中包含DDL for Locations,例如南,北或西。

所以我正在寻找的例子应该在前面看起来像这样:

<form id="Form1" runat="server">

        <h4>Select a file to upload:</h4>

        <asp:FileUpload id="FileUpload1"                 
            runat="server">
        </asp:FileUpload>

       <asp:FileUpload id="FileUpload2"                 
            runat="server">
        </asp:FileUpload>

        <asp:FileUpload id="FileUpload3"                 
            runat="server">
        </asp:FileUpload>

        <br /><br />

        <asp:Button id="UploadButton" 
            Text="Upload file"
            OnClick="UploadButton_Click"
            runat="server">
        </asp:Button>     

        <%--Plus however many more instances I need...--%>

        <hr />

        <asp:Label id="UploadStatusLabel"
            runat="server">
        </asp:Label>   

    </form>

背后的代码将是这样的:

 protected void  UploadButton_Click(object sender, EventArgs e)
    {
        // Before attempting to save the file, verify
        // that the FileUpload control contains a file.
        if (FileUpload1.HasFile) 
          // Call a helper method routine to save the file.
          SaveFile(FileUpload1.PostedFile);
        elseif(FileUpload2.HasFile)
           // Call a helper method routine to save the file.
          SaveFile(FileUpload2.PostedFile);
        elseif(FileUpload3.HasFile)
        // Call a helper method routine to save the file.
          SaveFile(FileUpload3.PostedFile);
        else
          // Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

      void SaveFile(HttpPostedFile file)
      {            
        // Specify the path to save the uploaded file to.
        string savePath = "c:\\temp\\uploads\\";

        // Get the name of the file to upload.
        string fileName = FileUpload1.FileName;

        // Create the path and file name to check for duplicates.
        string pathToCheck = savePath + fileName;

        // Create a temporary file name to use for checking duplicates.
        string tempfileName = "";

        // Check to see if a file already exists with the
        // same name as the file to upload.        
        if (System.IO.File.Exists(pathToCheck)) 
        {
          int counter = 2;
          while (System.IO.File.Exists(pathToCheck))
          {
            // if a file with this name already exists,
            // prefix the filename with a number.
            tempfileName = counter.ToString() + fileName;
            pathToCheck = savePath + tempfileName;
            counter ++;
          }

          fileName = tempfileName;

          // Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name already exists." + 
              "<br />Your file was saved as " + fileName;
        }
        else
        {
          // Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully.";
        }

        // Append the name of the file to upload to the path.
        savePath += fileName;

        // Call the SaveAs method to save the uploaded
        // file to the specified directory.
        FileUpload1.SaveAs(savePath);

      }

这是做到这一点的方式还是我在这里遗漏了什么?

1 个答案:

答案 0 :(得分:1)

像这样更改你的代码

if (FileUpload1.HasFile) 
   // Call a helper method routine to save the file.
   SaveFile(FileUpload1.PostedFile);
if(FileUpload2.HasFile)
   // Call a helper method routine to save the file.
   SaveFile(FileUpload2.PostedFile);
if(FileUpload3.HasFile)
   // Call a helper method routine to save the file.
   SaveFile(FileUpload3.PostedFile);
else
   // Notify the user that a file was not uploaded.
   UploadStatusLabel.Text = "You did not specify a file to upload.";

您的每个fileupload都可以file,因此如果您使用esle if,则只有一个条件将首次运行true