Home > OS >  After upload show uploaded file in Asp.net
After upload show uploaded file in Asp.net

Time:02-24

I have this Asp.net code:

<asp:Panel ID="pnlCustomer" runat="server">
  <p >
    <b>Edit report</b></p>
  <table  id="Bug" cellspacing="0" cellpadding="2" width="100%" border="0">

    <tr>
      <td  width="25%">
        <asp:Label CssClass="CasualForm" id="lblUploadFile" runat="server">Save new file</asp:Label>
      </td>
      <td  width="25%">
        <asp:FileUpload CssClass="formdata100" ID="fuUploadedFile" runat="server" />
      </td>
      <td  width="50%" colspan="2">&nbsp;</td>
    </tr>

    <tr>
      <td  width="25%">
        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="BtnDeleteReport" style="float:left" OnClientClick="if(confirm('Do you really want to delete this report?')) {this.disabled = true;} else {return false;}" UseSubmitBehavior="false" />
      </td>
      <td  width="25%">&nbsp;</td>
      <td  width="47%">
        <asp:Button ID="btnDownloadFile" runat="server" Text="Get Report" onclick="btnDownloadFile_Click" />
      </td>

      <td  width="3%">
        <asp:Button ID="btnSave" runat="server" Text="Edit" OnClientClick="if(checkFileUploadSize()) {return true;}" onclick="btnEdit_Click" CommandArgument="Edit" />
      </td>
    </tr>

  </table>
</asp:Panel>

and this is backcode C#:

protected void btnEdit_Click(object sender, EventArgs e) {
  Button btnThis = (Button) sender;
  if (btnThis.Text == "Edit") {
    Edit();
    btnThis.Text = "Save";
  } else if (btnThis.Text == "Save") {
    Save();
    //btnThis.Text = "Edit";
  }
}


private void Save() {
  ReadDataFromGUI();

  // insert/update report in DB.
  int _id = reportsHandler.Update(report);
  Response.Redirect("~/ReportsEditor.aspx?id="   _id);
}


private void ReadDataFromGUI() {
  if (report == null)
    report = new Support_Report();
  report.id = report_id;
  report.id_entity = int.Parse(ddlEntities.SelectedValue);
  report.inactive = cbInactive.Checked;
  report.name = txtName.Text.Trim();
  report.description = txtDescription.Text.Trim();
  report.report_condition = txtReportCondition.Text.Trim();

  int _so;
  isSortOrderInteger = int.TryParse(txtSortOrder.Text.Trim(), out _so);
  if (isSortOrderInteger) {
    report.sort_order = _so;
  }

  string StrFileName = Path.GetFileName(fuUploadedFile.FileName);

  int IntFileSize = fuUploadedFile.PostedFile.ContentLength;

  if (StrFileName != null && StrFileName != "") {
    string path = Utility.Utility.GenerateTempFileName(StrFileName);
    FileInfo fi = new FileInfo(path);
    fuUploadedFile.PostedFile.SaveAs(path);

    try {
      var extension = Path.GetExtension(StrFileName).Replace(".", "");
      report.report_type = extension;
      if (extension == "mrt") {
        var xml_doc = new XmlDocument();
        xml_doc.Load(path);
        report.report_file = xml_doc.OuterXml;
      } else {
        report.report_file = Convert.ToBase64String(File.ReadAllBytes(path));
      }
    } finally {
      fi.Delete();
    }
  }
}

When I insert a file and want it to upload it looks like this: Before But when I press the "save" button then that file is deleted on the page and refreshed the page. Still, that file is in the database but I can't see it on that page. Then it looks like this after saving: After Why is this happening? How to fix that it always shows me even after the update?

CodePudding user response:

The file upload control can ONLY survie ONE post back. When you click on ANY button that causes a post-back, then the file upload REALLY starts. And once that is done, then your code has to grab save that information about the file. When code behind is now done, the file-upload control is re-set, and the file can no longer be had, looked at, or used by the upload control.

And I not really sure of the logic of having a "edit" button. Why not just have a single save or submit button. I mean, some edit button not going to always be hit by the user - they will just start typing into the text boxes, and then hit save. But as noted, any button click (post-back) will cause the up-load to start, and then as noted, in that post back you have ONE chance to get that information.

With the introduciton fo a edit button, then a user might use the file upload to select a file, then say hit edit - that's going to trigger the post-back.

As a result, you need ONE save button to de-confuse the UI, but more important, on that save, you have to save that file, update your database, and in fact probably want to either hide the up-load control, or move on in your UI from the up-loader page to some other display of the information. So, you want user to enter information, select a file, and then hit ONE button to start the up-load, and then save the information entered. If you allow additonal files to be up-loaded, then want to save/setup some hidden field, or some such to keep track of that issue.

  • Related