简体   繁体   English

如何从AjaxFileUpload获取base64图像?

[英]How to get base64 image from AjaxFileUpload?

I am trying to get the images put into an AjaxFileUpload as base64 strings. 我试图将图像作为base64字符串放入AjaxFileUpload中。 Would I do this in OnUploadCompleteAll? 我会在OnUploadCompleteAll中这样做吗?

    protected void AjaxFileUpload1_UploadCompleteAll(object sender, AjaxFileUploadCompleteAllEventArgs e)
    {

    }

In aspx: 在aspx中:

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="jpg,jpeg,png,gif" MaximumNumberOfFiles="5" OnUploadCompleteAll="AjaxFileUpload1_UploadCompleteAll"></asp:AjaxFileUpload>

The UploadCompleteAll event fires when all files (in a multi-upload) have completed. 所有文件(多次上传)均已完成时,将触发UploadCompleteAll事件。

There is also another event that fires once for each file uploaded, called UploadComplete . 还有一个事件为每个上传的文件触发一次,称为UploadComplete You should handle that event. 您应该处理该事件。

// in ASPX page
<asp:AjaxFileUpload ID="AjaxFileUpload1" 
                    runat="server"
                    AllowedFileTypes="jpg,jpeg,png,gif"
                    MaximumNumberOfFiles="5"
                    OnUploadCompleteAll="AjaxFileUpload1_UploadCompleteAll"
                    OnUploadComplete="AjaxFileUpload1_UploadComplete">
</asp:AjaxFileUpload>

And in code behind add something like this: 在后面的代码中添加以下内容:

// in code-behind
protected void AjaxFileUpload1__OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{       
    if (e.ContentType.Contains("jpg") || file.ContentType.Contains("jpeg")
            || file.ContentType.Contains("png") || file.ContentType.Contains("gif"))
    {           
        // file.FileName contains the name of the file
        fileUploader.SaveAs(MapPath("path/where/you/want/to/save/file"));

        // once saved, you can further manipulate the file if you wish

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM