AjaxFileUpload获取文件流

时间:2012-11-15 07:06:06

标签: c# asp.net c#-4.0

我希望像这样使用AjaxFileUpload获取文件System.IO.Stream。这是一个有条理的FileUpload组件。

System.IO.Stream theStream = fileUpload.PostedFile.InputStream;

有人可以告诉我该怎么做吗?

3 个答案:

答案 0 :(得分:6)

如果您正在处理UploadComplete事件,那么您可以从GetStreamContents(作为流)和GetContents(作为字节数组)方法获取{{{ 1}}事件参数

e.g。

AjaxFileUploadEventArgs

您可以看到AjaxFileUploadEventArgs的源代码中可用的内容 - 这比文档中的内容要多得多。

答案 1 :(得分:2)

您可以暂时保存文件,然后阅读它以获取FileStream。它可能有点脏,但它对我有用。

VB.NET:

Dim tempPath As String = System.IO.Path.GetTempFileName()
yourAjaxFileUpload.SaveAs(tempPath)
Using fs As FileStream = File.OpenRead(tempPath)
    'Do stuff with fs here
End Using
File.Delete(tempPath)

C#:

string tempPath = System.IO.Path.GetTempFileName();
yourAjaxFileUpload.SaveAs(tempPath);
using (FileStream fs = File.OpenRead(tempPath)) {
    //do stuff with fs here
}
File.Delete(tempPath);

答案 2 :(得分:0)

如果有人想在保存图像之前进行转换(例如:Png到jpg).Below可能对 ajaxcontroltoolkit

中的 asyncfileuploader 提供帮助
   Protected Sub FileUploadComplete(ByVal sender As Object, ByVal e As EventArgs)
    Try
        Dim id As String = Session("MBRId")
        Dim contentType As String = AsyncFileUploadProfileImage.ContentType
        If (contentType = "image/jpeg" Or contentType = "image/png") Then
            Dim filename As String = System.IO.Path.GetFileName(AsyncFileUploadProfileImage.FileName)
            Dim TOjpegImage = System.Drawing.Image.FromStream(AsyncFileUploadProfileImage.FileContent)
            TOjpegImage.Save(Server.MapPath("img_prof/" + id + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim Ext As String = System.IO.Path.GetExtension(AsyncFileUploadProfileImage.FileName)
            'Commented working save due to conversion of image above
            ' AsyncFileUploadProfileImage.SaveAs(Server.MapPath("img_prof/") + id + Ext)
            lblMesg.Text = "File Uploaded Successfully"


        Else
            lblMesg.Text = "We support only images of type jpg and png"
        End If
    Catch ex As Exception
        iHelperClasses.ErrorLogger.ErrorWriter("Method:FileUploadComplete()Error:" + ex.Message)
    End Try


End Sub