所以我有这段代码:
if ((uplImage.FileName != ""))
{
byte[] raw = new byte[10000];
//to allow only jpg gif and png files to be uploaded.
string extension = Path.GetExtension(uplImage.PostedFile.FileName.ToUpper());
if (((extension == ".JPG") || ((extension == ".GIF") || (extension == ".PNG"))))
{
DALBio bio = new DALBio();
FileStream fs = new FileStream(uplImage.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
fs.Read(raw, 0, System.Convert.ToInt32(fs.Length));
bio.PlayerID = Session["playerID"].ToString();
bio.Pending = 'Y';
bio.Photo = raw;
DALBio.insertImage(bio);
}
}
当我尝试这个时,流不会读取图像。 raw
永远无法获得图像。它保持为空并且在执行存储过程时被捕获,说我从未传递过图像。我相信代码很好。我不知道为什么我不能将图像放入我的字节数组中。
答案 0 :(得分:0)
我做的是获得ByteArray
:
public Byte[] GetArrayFromFile(string path)
{
Byte[] data = null
FileInfo fileInf = new FileInfo(path);
long numBytes = fileInf.Length;
FileStream fStream = nw FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(fStream);
data = bReader.ReadBytes((int)numBytes);
return data;
}
然后它使用Entity Framework正确地存储在数据库中(如果您的DAL
在数据库中正确插入对象,它应该可以正常工作。)
答案 1 :(得分:0)
您可以将raw
数组创建/定义为
FileStream fs = new FileStream(uplImage.PostedFile.FileName,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.Read);
raw = new byte[fs.Length];
// same code as above..
您也可以尝试类似的代码
System.IO.Stream myStream;
Int32 fileLen;
// Get the length of the file.
fileLen = uplImage.PostedFile.ContentLength;
// Create a byte array to hold the contents of the file.
Byte[] input = new Byte[fileLen];
// Initialize the stream to read the uploaded file.
myStream = uplImage.FileContent;
// Read the file into the byte array.
myStream.Read(input, 0, fileLen);
// input will hold the byte array