-1

I have a controller method, which takes HttpPostedFileBase picture as an argument. Of course, it is used for loading images from a form. But now I want to load the file from another method, using that method. Can I make HttpPostedFileBase file in code, using path to image? Or may be another solution is preferred?

Okay, code:

public ActionResult UploadPicture(HttpPostedFileBase picture)
{
    if (picture.ContentLength > Convert.ToInt32(ConfigurationManager.AppSettings["MaxMBFileSize"]) * 1024 * 1024 || picture.ContentLength == 0)
    {
        return ClientError(ErrorCodes.ClientErrorCodes.FileSizeError, "File size is incorrect");
    }
    else
    {
        string contentType = picture.ContentType;

        if (!PictureHelper.ContentTypeIsValid(contentType))
        {
            return ClientError(ErrorCodes.ClientErrorCodes.MimeTypeError, "Incorrect file type");
        }
        else
        {
            string pictureName = Guid.NewGuid().ToString();
            pictureName += picture.FileName.Substring(picture.FileName.LastIndexOf('.'));

            string serverPath = AppDomain.CurrentDomain.BaseDirectory;
            picture.SaveAs(serverPath + ConfigurationManager.AppSettings["LocalImagesPath"] + pictureName);

            return Success(new { pictureName = pictureName });
        }
    }
}

Really, body absolutely doesn't matter. Of course I have something like:

<form method="post" action="Photo\UploadPicture">
    <input type="file">
    <input type="submit" value="submit">
</form>

And I want something like:

public ActionResult NewMethod()
{
    string path = ""; // real path to file here
    var file = OhMyGodMagicTransfer(path);
    // sending request
    request.attach(file);
    request.send;
}
John H
  • 14,422
  • 4
  • 41
  • 74
lenden
  • 800
  • 2
  • 14
  • 38
  • 1
    You say you have a method and you'd like to reuse it. I would say the answer is probably "sure, why not? Code reuse is a good thing!" But you post no details about the method, how it is already used, and what the method actually does, so it's impossible to say. – Dan Puzey Aug 06 '12 at 09:26
  • When user posts file using form, it is posted to first method with HttpPostedFileBase argument. It is obvious. I want to do the same thing in the second method, as the form is used. It is very simple just to call to first method, or give some simple arguments, but I want to know how to give file-arguments. Creating HttpPostedFileBase object may be solution, but I don't know is it real – lenden Aug 06 '12 at 09:30
  • It may be obvious to you, but nothing is obvious to those of us reading who can't see your code. What do you mean by "file-arguments?" What do you mean by "is it real?" – Dan Puzey Aug 06 '12 at 09:32

2 Answers2

2

If the file has already been saved to disk then just return a FilePathResult :

 public ActionResult FileDownload()
        {
            var fileLocation = "C:\file.jpg"; 
            var fileType = "image/jpeg" //this is the Mime content type;
            return File(fileLocation , fileType ); 
        }

Check out the overloads for File as you can return a filestream or byte array.

Judo
  • 5,167
  • 3
  • 24
  • 34
  • And how to use it in first method? HttpPostedFileBase picture = File(fileLocation, "application/pdf"); doesn't work – lenden Aug 06 '12 at 11:01
  • Just edited the answer. In your case simply use the line 'return File(path, "image/jpg");' . Assuming the file is a jpg file or just edit the file type to the appropriate Mime type http://www.webmaster-toolkit.com/mime-types.shtml – Judo Aug 06 '12 at 11:05
  • "return" has no bearing on the case. I need to post-send my file to other url. in which connection is return here? – lenden Aug 06 '12 at 11:09
  • Not sure I understand you, but the url in my answer would be /FileDownload (depending on your routing) and requesting this url from the browser will download the file. – Judo Aug 06 '12 at 11:16
  • I don't need to open anything in browser, it's just code... I have class ApiMethod, it has field "body, where I need to write smth like "{ picture = null, smthelse = 123" }. Code tests this method, sending body to some url. Controller method, which request is send to has some arguments, here HttpPostedFileBase picture and int smthelse. I need to put not null value to that body.. Real image – lenden Aug 06 '12 at 11:22
  • So what are you looking to do? You saved the image to disk, what should this controller do? Return the file? or process it... – Judo Aug 06 '12 at 11:36
  • It doesn't matter, what what should it do after that. Task is to get an object of ApiMethod class with field body with some image(it is saved to disk, I just have url, doesn't matter). Then, I should do something with that object and return maybe something, that has no connection to that image – lenden Aug 06 '12 at 11:42
0

   public ActionResult Index()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\proba\MvcApplication3\MvcApplication3\XMLFile1.xml");
            List<Class1> pitanja = new List<Class1>();
            Random rand = new Random();
            int broj1 = rand.Next(1, 3);
            int broj2 = rand.Next(3, 5);
            int broj3 = 5;

            TempData["odg1"] = broj1;
            TempData["odg2"] = broj2;
            TempData["odg3"] = broj3;

            string odg1 = Request.Form["odgg1"];
            string odg2 = Request.Form["odgg2"];
            string odg3 = Request.Form["odgg3"];

            TempData["korisodg1"] = odg1;
            TempData["korisodg2"] = odg2;
            TempData["korisodg3"] = odg3;


            foreach (XmlNode cvor in doc.SelectNodes("/kviz/Pitanje"))
            {

                if (broj1.ToString() == cvor["Rbr"].InnerText)
                {
                    pitanja.Add(
                        new Class1
                        {
                            pitanje = cvor["Pitanje"].InnerText,
                            odg1 = cvor["Odgovor1"].InnerText,
                            odg2 = cvor["Odgovor2"].InnerText,
                            odg3 = cvor["Odgovor3"].InnerText,
                            odg4 = cvor["Odgovor4"].InnerText,
                            resenje = cvor["Resenje"].InnerText
                        }
                        );
                }
                else if (broj2.ToString() == cvor["Rbr"].InnerText)
                {
                    pitanja.Add(
                        new Class1
                        {
                            pitanje = cvor["Pitanje"].InnerText,
                            odg1 = cvor["Odgovor1"].InnerText,
                            odg2 = cvor["Odgovor2"].InnerText,
                            odg3 = cvor["Odgovor3"].InnerText,
                            odg4 = cvor["Odgovor4"].InnerText,
                            resenje = cvor["Resenje"].InnerText
                        }
                        );
                }
                else if (broj3.ToString() == cvor["Rbr"].InnerText)
                {
                    pitanja.Add(
                        new Class1
                        {
                            pitanje = cvor["Pitanje"].InnerText,
                            odg1 = cvor["Odgovor1"].InnerText,
                            odg2 = cvor["Odgovor2"].InnerText,
                            odg3 = cvor["Odgovor3"].InnerText,
                            odg4 = cvor["Odgovor4"].InnerText,
                            resenje = cvor["Resenje"].InnerText
                        }
                        );
                }

            }
            return View(pitanja);

        }

        public ActionResult operater()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\proba\MvcApplication3\MvcApplication3\XMLFile1.xml");
            List<Class1> pitanja = new List<Class1>();

            foreach (XmlNode cvor in doc.SelectNodes("/kviz/Pitanje"))
            {
                if (TempData["odg1"].ToString() == cvor["Rbr"].InnerText)
                    pitanja.Add(new Class1
                    {
                        resenje = cvor["Resenje"].InnerText,
                        pitanje = cvor["Pitanje"].InnerText,
                        odgKorisnika = TempData["korisodg1"].ToString()
                    });

                else if (TempData["odg2"].ToString() == cvor["Rbr"].InnerText)
                    pitanja.Add(new Class1
                    {
                        resenje = cvor["Resenje"].InnerText,
                        pitanje = cvor["Pitanje"].InnerText,
                        odgKorisnika = TempData["korisodg2"].ToString()
                    });
                else if (TempData["odg3"].ToString() == cvor["Rbr"].InnerText)
                    pitanja.Add(new Class1
                    {
                        resenje = cvor["Resenje"].InnerText,
                        pitanje = cvor["Pitanje"].InnerText,
                        odgKorisnika = TempData["korisodg3"].ToString()
                    });
            }

            return View(pitanja);
        }

    }
}
@*@{
    ViewBag.Title = "Index";
    }
@Html.ActionLink("Operater", "operater", "home")
@Html.ActionLink("Nova pitanja","index","home")
@using(Html.BeginForm("Index","home")){
    
<h2>Index</h2>

    foreach (MvcApplication3.Models.Class1 pitanje in Model)
{
    
    @pitanje.pitanje;
    <br />
    @pitanje.odg1;<br />
    @pitanje.odg2;<br />
    @pitanje.odg3;<br />
    @pitanje.odg4;<br />
    <hr />
}
<input type="text" name="odgg1"  /><br />
<input type="text" name="odgg2" /><br />
<input type="text" name="odgg3" /><br />
<input type="submit" value ="Potvrdi" />
}*@
@*@{
    ViewBag.Title = "operater";
}
@Html.ActionLink("Nova pitanja","index","home")

<h2>operater</h2>

@foreach (MvcApplication3.Models.Class1 pitanj in Model)
{
    @pitanj.pitanje;<br /> 
    @pitanj.resenje;<br /><br /><br />
    @pitanj.odgKorisnika;
    <hr />
}*@
Marko
  • 18
  • 2
  • thank you for sharing your code sample. It would be very helpful if you could add a few comments to it to explain the process. Thanks. – Kate Orlova Jun 02 '19 at 12:06