Friday, 15 March 2013

c# - Store file temporary -



c# - Store file temporary -

this question has reply here:

how temporary folder current user 4 answers where can write temp file asp.net? 1 reply

i have code drag&drop-function images. problem saves file computer @"c:\users\xxxx\desktop\ temporarily. problem because means user cant utilize function. how can adjust code in order create work online?

i somehow need find way stores 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 uploads img imahehost(dunno if 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(); homecoming result; }

have @ path.gettemppath() path.gettempfilename() can help you.

a quick solution be:

// var fullpath = @"c:\users\xxxx\desktop\" + filename; // not good, since user folder var fullpath = path.gettempfilename(); // temp file, unqiue name

the reason recommend path.gettempfilename() rather path.gettemppath() + filename filename doesn't matter when upload file-bytes, , guarantees unique filename impossible have filename clashes.

var fullpath = path.gettemppath() + filename; // no good, clash if filenames same

c#

No comments:

Post a Comment