我建立那个班级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace CDN
{
public class Image
{
#region Image parameters
private int _PG_ID;
private string _PG_FileName;
private int _PG_ItemId;
private int _TD_ID;
private Boolean _PG_Visible;
private string _PG_Caption;
public int PG_ID
{
get { return _PG_ID; }
set { _PG_ID = value; }
}
public string PG_FileName
{
get { return _PG_FileName; }
set { _PG_FileName = value; }
}
public int PG_ItemId
{
get { return _PG_ItemId; }
set { _PG_ItemId = value; }
}
public int TD_ID
{
get { return _TD_ID; }
set { _TD_ID = value; }
}
public Boolean PG_Visible
{
get { return _PG_Visible; }
set { _PG_Visible = value; }
}
public string PG_Caption
{
get { return _PG_Caption; }
set { _PG_Caption = value; }
}
#endregion
public Image(int PG_ID)
{
GetImage(PG_ID);
}
public Image()
{
}
private void GetImage(int PG_ID)
{
//TODO get image data from db and fill all parameters with the data
}
public int SaveImage(FileUpload fileUpload,string caption,Boolean visible,int toolId,int itemId)
{
try
{
SaveImageToTemporaryFolder(fileUpload);
return CreateImageOnDb(fileUpload.FileName, caption, visible, toolId, itemId);
}
catch (Exception)
{
throw;
}
}
private int CreateImageOnDb(string fileName, string caption, bool visible, int toolId, int itemId)
{
//TODO save image data on db return image id
return 0;
}
private void SaveImageToTemporaryFolder(FileUpload fileUpload)
{
try
{
string savePath = HttpContext.Current.Server.MapPath("~") + "\\upload\\pgallery\\";
fileUpload.SaveAs(savePath + fileUpload.FileName);
}
catch (Exception)
{
throw;
}
}
}
}
如何传递文件?
EDITED:
在函数SaveImage中,我传递一个FileUpload控件
我想用这样的课:
当上传图像以获取新的Image对象并将所有数据传递给他时,但我如何将其传输给他。
Image img = new Image();
img.PG_Caption = "my file name";
img.PG_Visible = True;
img.SaveImage(file);
答案 0 :(得分:0)
您可以更改SaveImage函数以获取Stream并传入,如下所示:
using (MemoryStream stream = new MemoryStream(fileUpload.FileBytes))
{
img.SaveImage(stream);
}