Forum Moderators: open
I have a String which I need to write directly to the OutputStream object in ASP.NET. I'm trying to create a Microsoft Outlook "VCard", and there are problems when I just create the file from text. Thus, I'd like to build the entire VCard in a StringBuilder object and put that directly into the OutputStream. But I think I need to convert the String or Stringbuilder to a Byte array in order to do that.
Does anyone know how to convert from a String to a Byte array? Or a better way to do this?
Thanks!
g.
Maybe a better question is whether you can change the filename of the current aspx page from within VbScript?
As it is, I'm produce perfectly workable files, except the file extension doesn't match the content type. I guess I thought that writing data directly to the OutputStream would change it. Could that make a difference?
using System;
using System.IO;
namespace myUtil
{
public class Utils
{
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
}
}
'Now I can use this class and get ArrayByte
Private Function getBinaryFile(ByVal sFile As String) As Byte()
Dim aFile As Byte()
If File.Exists(sFile) Then
Try
Dim s As FileStream = File.OpenRead(sFile)
Dim obj As New myUtil.Utils
aFile = obj.ConvertStreamToByteBuffer(s)
s.Close()
File.Delete(sFile)
obj = Nothing
Return aFile
Catch e As Exception
getBinaryFile = Nothing
End Try
End If
End Function
Writing and serving the file is no problem, but I can't get the file to come across with a file extension that matches its ContentType. Interestingly, it does get the right file extension when I do the same thing in classic ASP. Does anyone know why?