Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file upload #60

Closed
bufferUnderrun opened this issue Jan 17, 2017 · 4 comments
Closed

Support file upload #60

bufferUnderrun opened this issue Jan 17, 2017 · 4 comments

Comments

@bufferUnderrun
Copy link
Contributor

Hi,

Do you have a code snippet to handle file uploading from a html form <input type="file" /> ?

Thanks,

@geoperez
Copy link
Member

geoperez commented Jan 17, 2017 via email

@bufferUnderrun
Copy link
Contributor Author

yep in https://github.com/unosquare/embedio/wiki/Cookbook you use the http://stackoverflow.com/a/21689347/3699737 code snippet. But i don't see how to pass the Stream data from embedio restapi to the async Task ParseFiles ?

@geoperez
Copy link
Member

This is a functional method that we use in other project with EmbedIO and the nuget HttpMultipartParser:

[WebApiHandler(HttpVerbs.Post, "/api/upload")]
        public bool UploadCert(WebServer server, HttpListenerContext context)
        {
            try
            {
                var parser = new MultipartFormDataParser(context.Request.InputStream);

                if (parser.Files.Any() == false)
                    throw new Exception("Invalid request, you need to post a file");

                using (var ms = new MemoryStream())
                {
                    parser.Files.First().Data.CopyTo(ms);
                    var path = Path.Combine(CurrentApp.EntryAssemblyDirectory, RelayService.Constants.DefaultCertName);
                    File.WriteAllBytes(path, ms.ToArray());
                    SettingsProvider<AppSettings>.Instance.Global.SmtpServerCertificateFile = path;
                    SettingsProvider<AppSettings>.Instance.Global.SmtpServerCertificatePassword = parser.Parameters.First().Data;
                    SettingsProvider<AppSettings>.Instance.PersistGlobalSettings();
                }

                Task.Factory.StartNew(() => StartupService.Current.Restart());
                context.Response.Redirect("/");
                return true;
            }
            catch (Exception ex)
            {
                return HandleError(context, ex);
            }
        }

@geoperez
Copy link
Member

geoperez commented Feb 6, 2017

Closing ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants