I need to save a Bitmap as a jpeg file on my website which is hosted on Hostgator, via WCF service.
Code that needs to save file looks like this
Bitmap bmPhoto = ...
var path = "~/httpdocs/Images/" + filename + ".jpg";
bmPhoto.Save(HostingEnvironment.MapPath(path), ImageFormat.Jpeg);
On my PC I'm using Server.MapPath and it works perfectly well. But I can't use Server.MapPath in WFC service. Instead I use HostingEnvironment.MapPath in System.Web.Hosting and it doesn't work. What do I need to do to get it to work?
Related
I am trying to download a video file (MP4) sitting in Cisco Webex server with C#. The URL I have is a download URL which is not exactly file location. Download URL is actually getting browser to download video file.
I tried 'WebClient' but with no luck. Mine is a console application, therefore I cannot use 'HttpWebRequest' and add the MIME type to IIS. Below is the code that I tried using:
WebClient client = new WebClient();
client.DownloadFile("https://webex.com/lsr.php?RCID=9853e32d921d", #"C:\\Video.mp4");
For now, with my code I am starting an instance of the browser with 'Process.Start("URL")' and have changed the default download location. I know this is not the correct solution therefore I request suggestions.
I don't know if you're still looking for an answer to this or not but I was recently looking for something similar. I wanted to download a video from my web page to the client's browser from my server. I did this and it is successfully downloading.
var fileName = Path.GetFileName( filePath );
HttpResponseBase.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = type;
this.Response.AppendHeader( "Content-Disposition", "attachment; filename=" + fileName );
this.Response.BinaryWrite( System.IO.File.ReadAllBytes( filePath ) );
this.Response.End();
I hope it helps!
I've taken on an asp/c# web app to fix originally done by the previous developer at my workplace. The code shows a Gridview populated by results from a query showing a list of files, one column is made up of 'command fields' that when clicked download a file. Everything seems to go smoothly until it reaches the file download as it can't seem to find the file on the server. My C# really isn't strong so bear with me and if you need further info that I've missed out please do say so.
Here is the specific part of code that causes problems:
//strSuppDocName - is already declared elsewhere
string path = System.IO.Path.Combine(Server.MapPath("~/Documents/"), strSuppDocName);
if (!Directory.Exists(path)){
System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");
}
else {
System.Net.WebClient client = new System.Net.WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ClearContent();
Response.ClearHeaders();
FileInfo file = new FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "Attachment;FileName:" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(strExtSuppDoc.ToLower());
Response.WriteFile(file.FullName);
Response.End();
}
}
What happens when I run the code is that the grid view populates okay, I click the file to download and it enters the first branch of the if statement showing the path. Before I added in the if statement it was showing the following error: "could not find a part of the path". I've tried fiddling with the path such as setting it absolutely:
string path = System.IO.Path.Combine(#"E:\web\Attestation\Documents\", strSuppDocName);
And without using the Combine method above and using standard string concatenation with '+'. Any help or guidance is most appreciated, thanks!
You're mixing a handful of technologies here. First of all, this doesn't belong in a web application:
System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");
Web applications aren't Windows Forms applications. This won't display anything to someone using the web application, because there's no concept of a "message box" over HTTP.
More to the point, however, you're using path in two very different ways. Here:
Byte[] buffer = client.DownloadData(path);
and here:
FileInfo file = new FileInfo(path);
Is path a URL on the network or a file on the file system? It can't be both. The first line is treating it as a URL, trying to download it from a web server. The second line is treating it as a local file, trying to read it from the file system.
What is path and how are you looking to access it? If it's a URL, download it with the WebClient and stream it to the user. If it's a file, read it from the file system and stream it to the user. You can't do both at the same time.
If you are interacting with a path on a network (aka UNC path), you have to use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you're opening files, creating, updating and deleting files, opening directories and deleting directories on a network path, use Server.MapPath.
Example:
System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));
The answer in short is that the file name was incorrect.
Strangely or mistakenly the author of the code, when uploading a given file, added an extra extension so a file would be something like 'image.png' to start off with then when uploaded would become image.png.png. Why didn't I notice this before you may ask? Simply because the whole path wasn't shown in Windows XP (don't ask why I was using XP) when viewing it through the explorer window and I dismissed this issue long before - a big mistake! After trying to find the file by typing the address of the file into the windows explorer address bar and receiving an error that the file doesn't exist, yet I could plainly see it did, a colleague looked at for the file remotely using Windows 7 and we saw that the file was shown as 'image.png.png'. Thereafter the path to the file worked correctly.
I'm trying to add a bitmap to a json string I'm sending to a website.
To do this I'm trying to convert the bitmap into a base64string, but this string turns up to be empty if checked on the website.
This is the code I use to covert the bitmap into a base64string
Bitmap bmp = (Bitmap)Image.FromFile(#"C:\ProgramData\test.jpg", true);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
bmp.Save(mStream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = mStream.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
but base64String seems to be empty, yet if I make a bitmap out of it again and save that bitmap to my PC everything seems to be fine.
To do this I'm trying to convert the bitmap into a base64string, but
this string turns up to be empty if checked on the website.
Your code is working when running from console application (btw look at comments you can do it more easily). Problem most likely is that local file system outside your web application is not accessible by web application. Try to read file from your web application folder.
To read file outside of your application folder look at this answer ASP.NET - Reading and writing to the file-system, outside the application The easiest way is to impersonate your AppPool with user account with appropriate rights.
I have an ASP.NET MVC Web Site ,the user can upload a video and When Done It finish uploading I show him an image extracted from the Video,
To do this I used the FFMPEG exe to get a frame.
Everthing works well in the developement machine , when I use the test environement it does not work!!
I've given the read/write and execute permissions to following folders:
1. videos(folder that store uploaded video files)
2. thumbnails (folder that store the thumbnails of videos, captured by ffmpeg)
3.ffmpeg.exe file at root and given read/write execute permissions to that file also.
but it does not work.
var _converter = new ImageConvertor(#System.Configuration.ConfigurationManager.AppSettings["FFmpegExec"].ToString());
_converter.WorkingPath = Server.MapPath("~/VideoSamples");
OutputPackage oo = _converter.ConvertToFLV(videoFilepath);
FileStream outStream = System.IO.File.OpenWrite(Path.Combine(Server.MapPath("~/VideoSamples"), id.ToString() + ".flv"));
oo.VideoStream.WriteTo(outStream);
This Code Works on developement env but not in test env !!!
Any Ideas Please
Perhaps the main issue is where I am uploading to - I am currently using MediaFire to upload my files. I have tested with downloading files of both ".exe" as well as ".png" formats; neither seem to work for me though.
The issue that is constantly occurring for me:
When I attempt to download a file, (I will put the URL's of the two files at the end of my question), the amount of data retrieved is either far greater - or far less than the actual size of the file. For example, I uploaded a blank VB6 executable file which is 16kb. The downloaded file comes out to be nearly 60 kilobytes!
Some things to note:
1) Both files download with no problems through Chrome (and I'm assuming other browsers as well).
2) I have tried multiple methods of retrieving the data from the downloaded file (same result).
My Code:
// Create a new instance of the System.Net 'WebClient'
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
// PNG File
string url = #"http://www.mediafire.com/imageview.php?quickkey=a16mo8gm03fv1d9&thumb=4";
// EXE File (blank VB6 exe file # 16kb)
// string url = #"http://www.mediafire.com/download.php?nn1cupi7j5ia7cb";
// Destination
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\Test.png";
byte[] result = client.DownloadData(url);
MessageBox.Show(result.Length.ToString()); // Returns 57,000 something (over 3x larger than my EXE file!).
// Write downloaded data to desired destination
System.IO.File.WriteAllBytes(savePath, result);