I am working on WPF application. I want to post image I select from OpenFileDialog. and I want to return url
URL should be like this : http://localhost:52185/api/Images/123456.jpg
But my url : http://localhost:52185/api/Images/DD8609E3-58A0-E811-B814-D050994A8C7D
How can I save images to localhost?
Post Method :
[HttpPost]
public IHttpActionResult Post(ImageDTO request)
{
var buffer = Convert.FromBase64String(request.Image);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);
using( var content = new MultipartFormDataContent())
{
byte[] bytes = new byte[objFile.InputStream.Length + 1];
objFile.InputStream.Read(bytes, 0, bytes.Length);
var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = request.Name };
content.Add(fileContent);
objFile.SaveAs("http://localhost:52185/api/Images/1.jpg");
}
return Ok("http://imageurl");
}
Client :
private async void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
fileDialog.ShowDialog();
var img = Convert.ToBase64String(File.ReadAllBytes(fileDialog.FileName));
HttpClient cli = new HttpClient();
var url = "http://localhost:52185/api/Images";
var parameters = new Dictionary<string, string> { { "image", img } };
var encodedContent = new FormUrlEncodedContent(parameters);
var response = await cli.PostAsync(url, encodedContent).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show(response.ToString());
}
}
As you haven't mentioned at which stage you have difficulties, I would assume you have managed to read input stream correctly. You may still need to modify this code to fit your coding style.
string f = @"C:\yourfolder\yourfile.png"; // @"c:\yourAPIRoot\Images\1.jpg
using (var binaryReader = new System.IO.BinaryReader(objFile.InputStream))
{
byte[] fileData = binaryReader.ReadBytes(objFile.ContentLength);
using (System.IO.FileStream FileStream1 =
new System.IO.FileStream(f, System.IO.FileMode.Create,
System.IO.FileAccess.Write))
{
FileStream1.Write(fileData, 0, fileData.Length);
FileStream1.Close();
}
}
//return "http://localhost:52185/api/images/1.jpg"
return "http://yourdomain.com/yourfolder/yourfile.png"; //modify this to your return type
I think you cannot use the virtual path in SaveAs
method, also what is the purpose of bytes/fileContent
in your code? plus /api/
is kinda non-physical address, I am NOT sure whether this will work or not, anyhow, you can change your method to something like this,
[HttpPost]
public IHttpActionResult Post(ImageDTO request)
{
var buffer = Convert.FromBase64String(request.Image);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);
// full virtual path of the image name
// TODO: right now, request.Name is assumed to be the image file name
var virtualPath = string.Format("/api/Images/{0}.jpg", request.Name);
using( var content = new MultipartFormDataContent())
{
// **** I am NOT sure what is the purpose of bytes/fileContent here ****
byte[] bytes = new byte[objFile.InputStream.Length + 1];
objFile.InputStream.Read(bytes, 0, bytes.Length);
var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = request.Name };
content.Add(fileContent);
// here we are converting the virtual path to physical path
var physicalPath = Server.MapPath(virtualPath);
objFile.SaveAs(physicalPath);
}
// I guess here you want to return the newly created path for the image
return Ok(virtualPath);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.