public class AdminController : Controller
{
public HHCCEntities hc = new HHCCEntities();
public ActionResult ServicesView()
{
var x = hc.Services.ToList();
return View(x);
}
[Authorize]
public ActionResult AddServices(int id = 0)
{
if (id == 0)
{
return View(new Service());
}
else
{
Service s = new Service();
HttpResponseMessage response =
GlobalVariables.webapiclient.GetAsync("Services/" + id.ToString()).Result;
s = response.Content.ReadAsAsync<Service>().Result;
return View(s);
}
}
private byte[] GetBinaryFile(string filename)
{
byte[] bytes;
using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
}
return bytes;
}
[Authorize]
[HttpPost]
public ActionResult AddServices(Service s, HttpPostedFileBase ImageFile)
{
if (ImageFile != null)
{
s.Simage = new byte[ImageFile.ContentLength];
ImageFile.InputStream.Read(s.Simage, 0, ImageFile.ContentLength);
}
if (s.ID==0)
{
if (ImageFile == null)
{
string filename = "MVC_WEBAPI\\images\\patient.png";
在这里,我想要一些从服务器路径中选择图像并将其转换为二进制流的代码。
变量文件名包含已经保存在服务器中的图像的路径。
此路径引发错误。
我的问题是如何为项目中存储在图像文件夹中的图像获取正确的路径?
byte[] bytes = GetBinaryFile(filename);
}
HttpResponseMessage response = GlobalVariables.webapiclient.PostAsJsonAsync("Services", s).Result;
}
else
{
HttpResponseMessage response = GlobalVariables.webapiclient.PutAsJsonAsync("Services/" + s.ID, s).Result;
}
return View();
}
}
}