2013-04-08 55 views
2

我有一个带有图像表的SQL Server(2008 R2),表中有一个带有类型图像的列。我插入我的图片到使用使用ASP.NET Web服务从SQL Server检索图像

Stream fs = FileUpload1.PostedFile.InputStream; 
BinaryReader br = new BinaryReader(fs); 
Byte[] bytes = br.ReadBytes((Int32)fs.Length); 

//insert the file into database 
string strQuery = "insert into pm005.images(imageName, type, alt, img)" + " values (@Name, @ContentType, @Alt, @Data)"; 
SqlCommand cmd = new SqlCommand(strQuery); 

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; 
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype; 
cmd.Parameters.Add("@Alt", SqlDbType.VarChar).Value = TextBox1.Text; 
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
InsertUpdateData(cmd); 
lblMessage.ForeColor = System.Drawing.Color.Green; 
lblMessage.Text = "File Uploaded Successfully"; 

private Boolean InsertUpdateData(SqlCommand cmd) 
{ 
    // Open a connection the the SQL Server 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = "server=sql-server;integrated security=SSPI;database=pm005"; 

    cmd.CommandType = CommandType.Text; 
    cmd.Connection = con; 
    try 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
     return false; 
    } 
    finally 
    { 
     con.Close(); 
     con.Dispose(); 
    } 
} 

我相信这工作正常此列,因为插入返回true,我可以在数据库中看到的数据。

但是我不能为我的生活看到我应该如何让这个图像退出。具体来说,我想要的是能够调用我的Web服务,将它传递给imageID并将图像(仅图像)返回给我。就像使用PHP完成的一样。这是即时使用的代码。

[WebMethod] 
[ScriptMethod(UseHttpGet = true)] 
public System.Drawing.Image getImage(int imageID, string uName, string pword) 
{ 
    string str=""; 
try 
{ 
    SqlCommand cmd = getSQLCommand("SELECT img FROM pm005.images WHERE id="+imageID); 

byte[] Img = (byte[])cmd.ExecuteScalar(); 

    // Convert the image record to file stream and display it to picture control 
    str = Convert.ToString(DateTime.Now.ToFileTime()); 
    FileStream fs = new FileStream(str, FileMode.CreateNew, FileAccess.Write); 
    fs.Write(Img, 0, Img.Length); 
    fs.Flush(); 
    fs.Close(); 

} 
catch 
{ 

} 
finally 
{ 

} 

System.Drawing.Image theImage = System.Drawing.Image.FromFile(str); 
return theImage; 

} 

这给错误:

System.ArgumentException: The path is not of a legal form. 
    at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength) 
    at System.IO.Path.GetFullPathInternal(String path) 
    at System.IO.Path.GetFullPath(String path) 
    at System.Drawing.IntSecurity.UnsafeGetFullPath(String fileName) 
    at System.Drawing.IntSecurity.DemandReadFileIO(String fileName) 
    at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 
    at WebService2.Service1.getImage(Int32 imageID, String uName, String pword) 

我看了这么多的教程,并没有找到除“使用PHP”任何固体的答案。如果我必须我会,但肯定必须有办法在ASP.NET中做到这一点?

回答

0

尝试指定完整的文件路径:

FileStream fs = new FileStream(@"C:\Temp\YourFile.jpg", FileMode.CreateNew, FileAccess.Write); 

编辑:

而不是像你想这样做,在内存中使用它的声音文件系统:

public byte[] imageToByteArray(System.Drawing.Image imageIn) 
{ 
    MemoryStream ms = new MemoryStream(); 
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif); 
    return ms.ToArray(); 
} 

public Image byteArrayToImage(byte[] byteArrayIn) 
{ 
    MemoryStream ms = new MemoryStream(byteArrayIn); 
    Image returnImage = Image.FromStream(ms); 
    return returnImage; 
} 

。 ..

System.Drawing.Image theImage = null; 
try 
{ 
    SqlCommand cmd = getSQLCommand("SELECT img FROM pm005.images WHERE id="+imageID); 
    byte[] Img = (byte[])cmd.ExecuteScalar(); 
    theImage = byteArrayToImage(Img); 
} 
catch 
{ 

} 
finally 
{ 

}  
return theImage;  
} 
+0

没有完整路径,图像存储在数据库中 – 2013-04-08 03:40:44

+0

当您从数据库中检索图像时,试图将其写入磁盘。我可以在调用堆栈中看到它在这里失败:'System.Drawing.Image.FromFile'。你可以仔细检查你正在使用FileStream方法的第6重载,它需要参数中第一个参数的filePath? – 2013-04-08 03:43:24

+0

是的,但这只是因为教程告诉我。我不知道为什么我只是把时间传递给我。 – 2013-04-08 03:46:42

相关问题