Email Attachment No Longer Working As Custom Style

2019-09-11 04:28发布

问题:

I have an asp.net application which allows a file upload and I had this working correctly but now I have custom styled my file upload field it's no longer working and I can't figure out how to get it working again with my new styled field.

OLD HTML

<div class="form-group">
    <asp:Label ID="Label3" class="col-md-3 control-label" runat="server" Text="Upload"></asp:Label>
    <div class="col-md-3">
        <asp:FileUpload ID="fuAttachment" runat="server" class="fileupload"></asp:FileUpload>
    </div>
</div>

OLD Code Behind

    var file = fuAttachment.PostedFile;
    if (file != null && fuAttachment.PostedFile.FileName != "")
    {
        var content = new byte[file.ContentLength];
        file.InputStream.Read(content, 0, content.Length);
        Session["FileContent"] = content;
        Session["FileContentType"] = file.ContentType;
        Session["File"] = fuAttachment.FileName;
        Session["AttachmentProvided"] = "Yes";
    }

NEW HTML

    <div class="fileinput fileinput-new input-group" data-provides="fileinput">
        <div class="form-control" data-trigger="fileinput" style="background-color: #ededed">
            <span class="fileinput-filename"></span>
        </div>
        <span class="input-group-addon btn btn-default btn-file">
            <span class="fileinput-new">
                <span class="glyphicon glyphicon-folder-open" title="Click to select a file."></span>
            </span>
            <span class="fileinput-exists">
                <span class="glyphicon glyphicon-folder-open" title="Click to change the selected file."></span>
            </span>
            <input type="file" name="...">
        </span>
        <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">
            <span class="glyphicon glyphicon-remove" title="Remove selected file."></span>
        </a>
    </div>

I need to store in my session as this populates another page so I need similar code behind as I had

回答1:

Edit file input to :

<input type="file" id="fuAttachment" runat="server" />

Now you use fuAttachment from codebehind for access uploaded file.

Note: If runat="server" attribute is missing you don't able to control this input from codebehind.