1

I have this code for my drag&drop-function for images. The problem with it is that it saves the file to my computer @"C:\Users\xxxx\Desktop\ temporarily. This is a problem because it means that a user cant use this function. So how can i adjust my code in order to make it work online?

I somehow need to find a way that stores the image temporarily.

public ActionResult SaveUploadedFile(string test)
        {


            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];


                var filename = Path.GetFileName(file.FileName);

                var sourceimage = System.Drawing.Image.FromStream(file.InputStream);
                img = sourceimage;

                var dog = new Dog();
                byte[] tempImg = dog.imageToByteArray(sourceimage);

                var fullPath = @"C:\Users\xxxx\Desktop\" + filename;
                file.SaveAs(fullPath);



                string link = dog.UploadImage(fullPath);

                JObject o = JObject.Parse(link);

                string name = (string) o.SelectToken("data.link");

                System.IO.File.Delete(@"C:\Users\xxxx\Desktop\" + filename);
                var page = RavenSession.Load<DogContentPage>(test);
                page.Image = name;
                RavenSession.SaveChanges();



            }

The method that uploads img to imahehost(dunno if it is relevant):

    public string UploadImage(string xOut)
    {



    using (var w = new WebClient())
    {
        var values = new NameValueCollection
        {
            {"image", Convert.ToBase64String(File.ReadAllBytes(xOut))}
        };

        w.Headers.Add("Authorization", "Client-ID " + ClientId);
        var response = w.UploadValues("https://api.imgur.com/3/image", values);

        var sr = new StreamReader(new MemoryStream(response));

        string result = sr.ReadLine();
        return result;


    }
Mwigs
  • 231
  • 1
  • 14
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • 1
    you should use the users temp folder instead. So, instead of `@"C:\Users\xxxx\Desktop\"` use `System.Environment.GetEnvironmentVariable("TEMP")` or `System.IO.Path.GetTempPath()`... never hardcode file paths. – balexandre Jun 18 '14 at 08:26

3 Answers3

8

Have a look at Path.GetTempPath() as well as Path.GetTempFileName() which can help you.

A quick solution would be:

 // var fullPath = @"C:\Users\xxxx\Desktop\" + filename; // Not good, since user folder
 var fullPath = Path.GetTempFileName(); // Temp file, with unqiue name

The reason I would recommend Path.GetTempFileName() rather than Path.GetTempPath() + filename is that the filename doesn't matter when you only upload the file-bytes, and it guarantees a unique filename so it is impossible to have filename clashes.

var fullPath = Path.GetTempPath() + filename; // No good, could clash if the filenames were the same
flindeberg
  • 4,887
  • 1
  • 24
  • 37
2

Rather than using a hard-coded string, you should use the following to find the temporary folder in which to store temporary files:

string tempPath = System.IO.Path.GetTempPath();
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

2 solutions I think:

N°1: a task in the server that check, let's say 2 times a day, if any file have to be deleted

N°2: a procedure called by your application to verify the difference between Now and the expiration date of your file.

Hope this ideas can help.

Nicolas Henrard
  • 843
  • 8
  • 19