Silverlight 应用程序中的图像上传
Image Upload in Silverlight Application
当我尝试使用 Silverlight 应用程序将图像从我的本地驱动器上传到 ms sql 时,它抛出异常“不允许文件操作。访问路径 'userimage.png' 被拒绝 ”。如何解决这个问题?我不擅长 Silverlight。请帮助我。
我的代码是,
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string filename = ofd.File.Name;
FileInfo fInfo = new FileInfo(filename);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);
我在这里遇到错误,“FileInfo fInfo = new FileInfo(filename);
”
堆栈跟踪是,
at System.IO.FileSecurityState.EnsureState()
at System.IO.FileInfo.Init(String fileName, Boolean checkHost)
at System.IO.FileInfo..ctor(String fileName)
at AttendanceManagementSystem.InsertPopup.btnimg_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
终于,它现在工作正常了。工作代码如下。
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
bool? userClickedOK = ofd.ShowDialog();
if (userClickedOK == true)
{
System.IO.Stream fileStream = ofd.File.OpenRead();
FileStream fStream = ofd.File.OpenRead();
BinaryReader br = new BinaryReader(fStream);
byte[] imagedata = br.ReadBytes((int)fStream.Length);
}
现在字节数组“imagedata
”可以插入数据库了。
当我尝试使用 Silverlight 应用程序将图像从我的本地驱动器上传到 ms sql 时,它抛出异常“不允许文件操作。访问路径 'userimage.png' 被拒绝 ”。如何解决这个问题?我不擅长 Silverlight。请帮助我。
我的代码是,
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string filename = ofd.File.Name;
FileInfo fInfo = new FileInfo(filename);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);
我在这里遇到错误,“FileInfo fInfo = new FileInfo(filename);
”
堆栈跟踪是,
at System.IO.FileSecurityState.EnsureState() at System.IO.FileInfo.Init(String fileName, Boolean checkHost) at System.IO.FileInfo..ctor(String fileName) at AttendanceManagementSystem.InsertPopup.btnimg_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
终于,它现在工作正常了。工作代码如下。
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
bool? userClickedOK = ofd.ShowDialog();
if (userClickedOK == true)
{
System.IO.Stream fileStream = ofd.File.OpenRead();
FileStream fStream = ofd.File.OpenRead();
BinaryReader br = new BinaryReader(fStream);
byte[] imagedata = br.ReadBytes((int)fStream.Length);
}
现在字节数组“imagedata
”可以插入数据库了。