We have video clip storage and we want to provide url link to the end user for the clips so that end user can start downloading the clips by clicking on the url. But trick here is url link is not direct path to the clip file stored on the virtual folder but url contains clip ID. It might look like,
www.xyz.com/clipstore/ID
When end user click on the url, server should search for the clip in the clip storage, process the clip ( convert from one format to other format) and start download at end user location.
Can anyone please guide us how server can initiate download for the end user when url is not directly pointing to the file but ID of it.
We are using IIS 6 / 7 , C# on the server side. Client is silverlight based.
It is possible and quite straight forward in ASP.net MVC.
For the sake of example, I have hard coded the mime type to "image/png", but i should be according to file type.
public ActionResult clipstore(string id)
{
var path = GetFilePathByID(id);
StreamReader reader = new StreamReader(path);
var fileBytes = System.IO.File.ReadAllBytes(path);
FileContentResult file = File(fileBytes, "image/png");
return file;
}
Related
I've run into a problem in a situation where an image is deleted and another file with the same name is saved in its stead (i.e. the file is replaced). As shown below:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _ReplaceGeneral(int ID, HttpPostedFileBase file)
{
var dbpath = query.FindImages(ID);
var path = Server.MapPath(dbpath.ImageURL);
System.IO.File.Delete(path);
file.SaveAs(path);
TempData["Message"] = "Image Successfully Replaced!";
return RedirectToAction("EditGallery");
}
In my server the file is successfully replaced. However, when I launch the site, the previous image is displayed.
Notes:
The Image isn't saved in my database- it's saved on my Server and the URL is stored on the database.
I can physically see the file replaced in the server so I'm absolutely positive it's being replaced.
Question: How and why does Image A display on my website even after replacing it with Image B?
I haven't been able to find any resources that articulate why and how this issue occurs so I apologize if it's a duplicate.
This sounds like a browser caching issue. If the URL of the image does not change the browser will use the cached version and not fetch the image from the server.
You can get round this by getting the image files last modified date/time convert that to a number and add it to the image URL as a query string (e.g. &t=XXXXX). In this way when the image file changes the URL changes too. The browser will then load the new image.
Ultimately I'm trying to upload a document from the user's file system via MVC .NET web site to Google Drive, which utilizes a service account.
I'm not sure if I'm implementing the appropriate design to accomplish the upload but I am getting hung up on the path of the file to be uploaded.
Web
#Html.TextBox("file", "file", new { type = "file", id = "fileUpload" })
Controller
public ActionResult GoogleDriveList(GoogleDrivePageVM vm, HttpPostedFileBase file)
File _file = new File();
var _uploadFile = System.IO.Path.GetFileName(file.FileName);
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
Error occurs on the ReadAllBytes statement. It could not find file 'C:\Program Files (x86)\IIS Express\Map of Universe.txt'. The file name is correct but the path is not.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
... Google Drive file stuff goes here
Then upload the file from the stream.
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, body.MimeType);
request.Upload();
So, am I going down the right path using the HTML file helper? And if so, what's the trick to get the path to work correctly? Also, I want to be able to support file sizes up to 500 MB (if that makes a difference).
If your getting the filename from a user selected windows file explorer dialog, then you shouldn't be using the below as will just strip out the filename without the path into your upload file variable and I am assuming that bogus path is where your're running the code from, so ReadAllBytes is trying to read from that path with the filename
var _uploadFile = System.IO.Path.GetFileName(file.FileName)
just change so it has that full path and filename you need to use in ReadAllBytes
var _uploadFile = file.FileName
I have a camera connected to a network that provides images which can be accessed by IP. For example something like this.
http://170.1.2.3/image?camera=2
I get returned a jpg file, which I can easily display back on a webpage using normal html.
<img src="http://170.1.2.3/image?camera=2" />
But instead of simply showing the camera image as seen from the time of the page load, I also want to save this image locally to the server using an MVC4 application. In addition it would be nice to include a timestamp on the filename. I'm not having any luck finding an example that looks right to me.
Let's say I want to start with the most basic example:
string imgUrl = "http://170.1.2.3/image?camera=2";
savelocally(imgurl, "newfilename-"+DateTime.Now.ToString());
.. and savelocally() should place the file in "~/Uploads". How do I do this? All the examples I've come across have been for uploading a file from desktop. I don't know how to translate that into grabbing a remote image.
Update:
This is an incomplete answer, as pointed out by comments.
string url = "http://170.1.2.3/image?camera=2";
string localFilename = #"C:\Projects\MvcApplication1\MvcApplication1\Uploads\tofile.jpg";
using (WebClient client = new WebClient())
{
client.DownloadFile(url, localFilename);
}
It works, however I don't like the fact that the file path is an absolute one. I tried replacing localFilename with ~/Uploads/tofile.jpg but it results in an error on execution. If possible I want to avoid having a fixed location for the upload folder.
i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder
this is how i do it :
protected void DownloadData(string strFileUrlToDownload)
{
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);
storeStream.Flush();
string currentpath = System.IO.Directory.GetCurrentDirectory() + #"\Folder";
using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Close();
}
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?
Basically i want to download the contents of the folder whereby the contents are actually images.
First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).
See this link:
WebClient.DownloadFile Method (String, String)
Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.
Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:
string localFilePath = Path.Combine("MyDownloadFolder", imageName);
where imageName is a unique filename (with file extension) for that file.
I want to get the path of the Image which is saved in Client Machine. I know the Path and the file name of the Image. By using FileUpload i can do it, but without using fileupload is it possible to get the path of the file ??.
My Scenario is given below,
Public void ConverttoByte()
{
//Get the image path from web.config & this image is in client machine
string strConfig = #"C:\Manikandan\image\image1.jpg";
MemoryStream MS = new MemoryStream();
Byte[] data;
int fiFileSize;
System.Drawing.Image image;
image = System.Drawing.Image.FromFile(strConfig);
image.Save(MS, System.Drawing.Imaging.ImageFormat.Gif);
data = MS.ToArray();
CallDBMethod(data);
}
Here I converted the image as Byte and I called the CallDBMethod to insert this byte details to DB..
This image is available in client machine, but not in server machine..
So, how to i get this image path from client machine & how can i solve this?
Without the File upload control, It is not possible unless you create an Activex control (may not work in all browsers. User has to give permission to run this control).
In a web application, You can not take ( steal) any file from the user's computer without them making an action to do so( ex : selecting the file in the File Input control and clicking some upload button).
If you want the full file path of the file user selected in the file upload control, you can get by HttpPostedFile.FileName property which gives you the fully qualified name of the file on the client (Ex : C:\MySomeFolder\SomeFile.jpg).
string fullPath=FileUpload1.HttpPostedFile.FileName
Assuming FileUpload1 is the ID of the File Upload control.
According to my knowledge it is not possible. You can't get the client's image path. The browser does not allow us to get the path. Using fileupload control you can also not get the complete path.
A similar question by me
How to Get complete file path using file upload control in asp.net or any other way?